00001
00032 #include "linden_common.h"
00033 #include "llmediaengine.h"
00034
00035 #include "indra_constants.h"
00036 #include "llstring.h"
00037
00038
00039 LLMediaEngine* LLMediaEngine::sInstance = 0;
00040
00042
00043
00044 void LLMediaEngine::initClass()
00045 {
00046 llassert(!sInstance);
00047 sInstance = new LLMediaEngine();
00048 }
00049
00050
00051 void LLMediaEngine::updateClass(F32 volume)
00052 {
00053 llassert(sInstance);
00054 sInstance->setVolume(volume);
00055 }
00056
00057
00058 void LLMediaEngine::cleanupClass()
00059 {
00060 delete sInstance;
00061 sInstance = NULL;
00062 }
00063
00065
00066 LLMediaEngine::LLMediaEngine() :
00067 mAvailable( TRUE ),
00068 mEnabled( TRUE ),
00069 mAutoScaled( FALSE ),
00070 mUrl( "" ),
00071 mMediaRenderer( 0 ),
00072 mImageUUID( LLUUID::null ),
00073 mVolume( 0.0f ),
00074 mProxyEnabled ( FALSE ),
00075 mProxyAddress ( "" ),
00076 mProxyPort ( 3128 ),
00077 mProxySocks ( 5 ),
00078 mProxyExlude ( "" )
00079 {
00080 }
00081
00083
00084 LLMediaEngine::~LLMediaEngine()
00085 {
00086 unload();
00087 destroyImageRaw();
00088 }
00089
00091
00092 void LLMediaEngine::createImageRaw()
00093 {
00094 S32 width = getMediaRenderer()->getMediaWidth();
00095 S32 height = getMediaRenderer()->getMediaHeight();
00096 S32 depth = getMediaRenderer()->getMediaDepthBytes();
00097 if ((width > 0) && (height > 0) && (depth > 0))
00098 {
00099 if (mImageRaw.isNull())
00100 {
00101 mImageRaw = new LLImageRaw;
00102 }
00103 mImageRaw->resize(width, height, depth);
00104 mImageRaw->clear();
00105 }
00106 else
00107 {
00108 destroyImageRaw();
00109 }
00110 }
00111
00112 void LLMediaEngine::destroyImageRaw()
00113 {
00114 mImageRaw = NULL;
00115 }
00116
00118
00119 LLMediaEngine* LLMediaEngine::getInstance()
00120 {
00121 return sInstance;
00122 }
00123
00125
00126 LLMediaBase* LLMediaEngine::getMediaRenderer()
00127 {
00128 return mMediaRenderer;
00129 }
00130
00131
00133
00134 BOOL LLMediaEngine::init()
00135 {
00136 if( ! isAvailable() )
00137 return FALSE;
00138
00139 return TRUE;
00140 }
00141
00143
00144 BOOL LLMediaEngine::update()
00145 {
00146 BOOL res = FALSE;
00147 if( mMediaRenderer )
00148 {
00149 S32 result = mMediaRenderer->updateMedia();
00150 switch(result)
00151 {
00152 case LLMediaBase::updateMediaNeedsSizeChange:
00153
00154 handleSizeChangedRequest();
00155 res = TRUE;
00156 break;
00157
00158 case LLMediaBase::updateMediaNeedsUpdate:
00159 res = TRUE;
00160 break;
00161
00162 case LLMediaBase::updateMediaNoChanges:
00163 default:
00164 res = FALSE;
00165 break;
00166 }
00167 }
00168 return res;
00169 }
00170
00171
00173
00174 BOOL LLMediaEngine::load ( const LLString& urlIn, bool web_url, const LLString& path, S32 width_pixels, S32 height_pixels )
00175 {
00176 if( ! isAvailable() )
00177 return FALSE;
00178
00179 if( isLoaded() )
00180 return TRUE;
00181
00182 this->unload();
00183
00184 mMediaRenderer = LLMediaBase::make( LLMediaBase::QuickTime, width_pixels, height_pixels);
00185
00186 if( ! mMediaRenderer )
00187 return FALSE;
00188
00189 if( ! mMediaRenderer->init() )
00190 {
00191 delete mMediaRenderer;
00192 mMediaRenderer = 0;
00193 return FALSE;
00194 }
00195
00196
00197 if( mMediaRenderer )
00198 mMediaRenderer->setAutoScaled( mAutoScaled );
00199
00200 if( ! mMediaRenderer->load( urlIn ) )
00201 {
00202 delete mMediaRenderer;
00203 mMediaRenderer = 0;
00204 return FALSE;
00205 }
00206
00207 return TRUE;
00208 }
00209
00211
00212 BOOL LLMediaEngine::isLoaded()
00213 {
00214 if( ! isAvailable() )
00215 return FALSE;
00216
00217 if( mMediaRenderer )
00218 return mMediaRenderer->isLoaded();
00219 else
00220 return FALSE;
00221 }
00222
00224
00225 BOOL LLMediaEngine::unload()
00226 {
00227 if( ! isAvailable() )
00228 return FALSE;
00229
00230 if( mMediaRenderer )
00231 {
00232 mMediaRenderer->stop();
00233 mMediaRenderer->unload();
00234 delete mMediaRenderer;
00235 mMediaRenderer = 0;
00236
00237
00238
00239 return TRUE;
00240 };
00241
00242 return FALSE;
00243 };
00244
00246
00247 BOOL LLMediaEngine::play()
00248 {
00249 if( ! isAvailable() )
00250 return FALSE;
00251
00252
00253
00254 this->setVolume( mVolume );
00255
00256 if( mMediaRenderer )
00257 if( ! mMediaRenderer->play() )
00258 return FALSE;
00259
00260 return TRUE;
00261 }
00262
00264
00265 BOOL LLMediaEngine::loop()
00266 {
00267 if( ! isAvailable() )
00268 return FALSE;
00269
00270
00271
00272 this->setVolume( mVolume );
00273
00274 if( mMediaRenderer )
00275 if( ! mMediaRenderer->loop( 0 ) )
00276 return FALSE;
00277
00278 return TRUE;
00279 }
00280
00282
00283 BOOL LLMediaEngine::pause()
00284 {
00285 if( ! isAvailable() )
00286 return FALSE;
00287
00288 if( mMediaRenderer )
00289 if( ! mMediaRenderer->pause() )
00290 return FALSE;
00291
00292 return TRUE;
00293 }
00294
00296
00297 BOOL LLMediaEngine::stop()
00298 {
00299 if( ! isAvailable() )
00300 return FALSE;
00301
00302 if( mMediaRenderer )
00303 if( ! mMediaRenderer->stop() )
00304 return FALSE;
00305
00306 return TRUE;
00307 }
00308
00310
00311 BOOL LLMediaEngine::seek (F64 time)
00312 {
00313 if( ! isAvailable() )
00314 return FALSE;
00315
00316 if( mMediaRenderer )
00317 if( ! mMediaRenderer->seek (time) )
00318 return FALSE;
00319
00320 return TRUE;
00321 }
00322
00324
00325 void LLMediaEngine::setAvailable( BOOL availableIn )
00326 {
00327 mAvailable = availableIn;
00328 }
00329
00331
00332 BOOL LLMediaEngine::isAvailable()
00333 {
00334 return mAvailable;
00335 }
00336
00338
00339 BOOL LLMediaEngine::setVolume( F32 volumeIn )
00340 {
00341 if( ! isAvailable() )
00342 return FALSE;
00343
00344 mVolume = volumeIn;
00345
00346 if( mMediaRenderer )
00347 {
00348 if( ! mMediaRenderer->setVolume( volumeIn ) )
00349 {
00350 return FALSE;
00351 };
00352 };
00353
00354 return TRUE;
00355 }
00356
00358
00359 void LLMediaEngine::setEnabled( BOOL enabledIn )
00360 {
00361 if( mAvailable )
00362 mEnabled = enabledIn;
00363 }
00364
00366
00367 BOOL LLMediaEngine::isEnabled()
00368 {
00369 if( mAvailable )
00370 return mEnabled;
00371 else
00372 return FALSE;
00373 }
00374
00376
00377 void LLMediaEngine::setAutoScaled( BOOL autoScaledIn )
00378 {
00379 mAutoScaled = autoScaledIn;
00380 }
00381
00383
00384 BOOL LLMediaEngine::isAutoScaled()
00385 {
00386 return mAutoScaled;
00387 }
00388
00390
00391 void LLMediaEngine::setUrl( const LLString& urlIn )
00392 {
00393 mUrl = urlIn;
00394 };
00395
00397
00398 const LLString& LLMediaEngine::getUrl ()
00399 {
00400 return mUrl;
00401 };
00402
00404
00405 void LLMediaEngine::setImageUUID( LLUUID imageUUIDIn )
00406 {
00407 mImageUUID = imageUUIDIn;
00408 };
00409
00411
00412 LLUUID LLMediaEngine::getImageUUID()
00413 {
00414 return mImageUUID;
00415 };
00416
00418
00419 void LLMediaEngine::handleSizeChangedRequest()
00420 {
00421 if( ! isAvailable() )
00422 return;
00423
00424
00425 LLMediaEngine::getInstance()->createImageRaw();
00426
00427
00428 if (mImageRaw.notNull())
00429 {
00430 LLMediaEngine::getInstance()->getMediaRenderer()->setBuffer( mImageRaw->getData() );
00431 }
00432
00433 }
00434
00436
00437 void LLMediaEngine::convertImageAndLoadUrl( bool enableLooping, bool web_url, const std::string& path)
00438 {
00439 LLMediaEngine* engine = LLMediaEngine::getInstance();
00440 LLString url = engine->getUrl();
00441 S32 width_pixels = 512;
00442 S32 height_pixels = 256;
00443 if (web_url)
00444 {
00445 width_pixels = 512;
00446 height_pixels = 512;
00447 }
00448
00449 bool success = false;
00450 if (engine->load( url, web_url, path, width_pixels, height_pixels ) )
00451 {
00452
00453 engine->createImageRaw();
00454
00455
00456 if (engine->getImageRaw())
00457 {
00458 engine->getMediaRenderer()->setBuffer( engine->mImageRaw->getData() );
00459 engine->getMediaRenderer()->setBufferSize(width_pixels, height_pixels);
00460
00461
00462 if( enableLooping )
00463 {
00464 engine->loop();
00465 }
00466 else
00467 {
00468 engine->play();
00469 }
00470 success = true;
00471 }
00472 }
00473
00474 if (!success)
00475 {
00476 llinfos << "MEDIA> unable to load " << LLMediaEngine::getInstance()->getUrl() << llendl;
00477
00478 }
00479 }
00480
00482
00483 void LLMediaEngine::process_parcel_media( LLMessageSystem *msg, void ** )
00484 {
00485
00486
00487
00488
00489 U32 flags;
00490 U32 command;
00491 F32 time;
00492 msg->getU32( "CommandBlock", "Flags", flags );
00493 msg->getU32( "CommandBlock", "Command", command);
00494 msg->getF32( "CommandBlock", "Time", time );
00495
00496 if (flags &( (1<<PARCEL_MEDIA_COMMAND_STOP)
00497 | (1<<PARCEL_MEDIA_COMMAND_PAUSE)
00498 | (1<<PARCEL_MEDIA_COMMAND_PLAY)
00499 | (1<<PARCEL_MEDIA_COMMAND_LOOP)
00500 | (1<<PARCEL_MEDIA_COMMAND_UNLOAD) ))
00501 {
00502
00503 if( command == PARCEL_MEDIA_COMMAND_STOP )
00504 {
00505
00506
00507 LLMediaEngine::getInstance()->stop();
00508 }
00509 else
00510
00511 if( command == PARCEL_MEDIA_COMMAND_PAUSE )
00512 {
00513
00514
00515 LLMediaEngine::getInstance()->pause();
00516 }
00517 else
00518
00519 if( command == PARCEL_MEDIA_COMMAND_PLAY )
00520 {
00521
00522
00523 convertImageAndLoadUrl( false, false, std::string() );
00524 }
00525 else
00526
00527 if( command == PARCEL_MEDIA_COMMAND_LOOP )
00528 {
00529
00530
00531
00532
00533 convertImageAndLoadUrl( true, false, std::string() );
00534 }
00535 else
00536
00537 if( command == PARCEL_MEDIA_COMMAND_UNLOAD )
00538 {
00539
00540
00541 if (LLMediaEngine::getInstance()->isLoaded())
00542 {
00543 LLMediaEngine::getInstance()->unload();
00544 LLMediaEngine::getInstance()->destroyImageRaw();
00545 }
00546 }
00547 }
00548
00549 if (flags & (1<<PARCEL_MEDIA_COMMAND_TIME))
00550 {
00551
00552 LLMediaEngine::getInstance()->seek(time);
00553 }
00554 }
00555
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572 void LLMediaEngine::process_parcel_media_update( LLMessageSystem *msg, void ** )
00573 {
00574 LLUUID media_uuid;
00575 char media_url[255];
00576 U8 auto_align;
00577 msg->getUUID( "DataBlock", "MediaID", media_uuid );
00578 msg->getString( "DataBlock", "MediaURL", 255, media_url );
00579 msg->getU8( "DataBlock", "MediaAutoScale", auto_align );
00580
00581 LLMediaEngine* media_engine = LLMediaEngine::getInstance();
00582 LLString url_string ( media_url );
00583
00584 auto_align = (auto_align) ? TRUE : FALSE;
00585
00586 if( !( (media_engine->getUrl() == url_string)
00587 && (media_engine->getImageUUID() == media_uuid)
00588 && (media_engine->isAutoScaled() == auto_align ) ) )
00589 {
00590 if (media_engine->isLoaded())
00591 {
00592 media_engine->unload();
00593 media_engine->destroyImageRaw();
00594 }
00595
00596 media_engine->setUrl(url_string);
00597 media_engine->setImageUUID(media_uuid);
00598 media_engine->setAutoScaled(auto_align);
00599 }
00600 }
00601
00602
00603 void LLMediaEngine::setNetworkProxy ( BOOL enabledIn, const LLString& addressIn,
00604 S32 portIn, S32 socksIn, const LLString& excludeIn )
00605 {
00606 mProxyEnabled = enabledIn;
00607 mProxyAddress = addressIn;
00608 mProxyPort = portIn;
00609 mProxySocks = socksIn;
00610 mProxyExlude = excludeIn;
00611 }
00612
00613
00614 void LLMediaEngine::getNetworkProxy ( BOOL& enabledOut, LLString& addressOut,
00615 S32& portOut, S32& socksOut, LLString& excludeOut )
00616 {
00617 enabledOut = mProxyEnabled;
00618 addressOut = mProxyAddress;
00619 portOut = mProxyPort;
00620 socksOut = mProxySocks;
00621 excludeOut = mProxyExlude;
00622 }
00623