aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmedia/llmediaengine.cpp
blob: c75ed3224c8efbe3259b94b38f19ad194f2ebb7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/** 
 * @file llmediaengine.cpp
 * @brief Top level media engine - wraps more specific functionality
 *
 * Copyright (c) 2005-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 */

#include "linden_common.h" 
#include "llmediaengine.h"

#include "indra_constants.h"
#include "llstring.h"

// singleton pattern - initialization
LLMediaEngine* LLMediaEngine::sInstance = 0;

//////////////////////////////////////////////////////////////////////////////

//static
void LLMediaEngine::initClass()
{
	llassert(!sInstance);
	sInstance = new LLMediaEngine();
}

//static
void LLMediaEngine::updateClass(F32 volume)
{
	llassert(sInstance);
	sInstance->setVolume(volume);
}

//static
void LLMediaEngine::cleanupClass()
{
	delete sInstance;
	sInstance = NULL;
}

//////////////////////////////////////////////////////////////////////////////
// default ctor 
LLMediaEngine::LLMediaEngine() :
	mAvailable( TRUE ),
	mEnabled( TRUE ),
	mAutoScaled( FALSE ),
	mUrl( "" ),
	mMediaRenderer( 0 ),
	mImageUUID( LLUUID::null ),
	mVolume( 0.0f ),
	mProxyEnabled ( FALSE ),
	mProxyAddress ( "" ),
	mProxyPort ( 3128 ),
	mProxySocks ( 5 ),
	mProxyExlude ( "" )
{
}

//////////////////////////////////////////////////////////////////////////////
// dtor 
LLMediaEngine::~LLMediaEngine()
{
	unload();
	destroyImageRaw();
}

//////////////////////////////////////////////////////////////////////////////
// create/destroy raw image
void LLMediaEngine::createImageRaw()
{
	S32 width = getMediaRenderer()->getMediaWidth();
	S32 height = getMediaRenderer()->getMediaHeight();
	S32 depth = getMediaRenderer()->getMediaDepthBytes();
	if ((width > 0) && (height > 0) && (depth > 0))
	{
		if (mImageRaw.isNull())
		{
			mImageRaw = new LLImageRaw;
		}
		mImageRaw->resize(width, height, depth);
		mImageRaw->clear();
	}
	else
	{
		destroyImageRaw();
	}
}

void LLMediaEngine::destroyImageRaw()
{
	mImageRaw = NULL; // deletes image
}

//////////////////////////////////////////////////////////////////////////////
//	retrieves the single instance of this class - based on singleton pattern
LLMediaEngine* LLMediaEngine::getInstance()
{
	return sInstance;
}

//////////////////////////////////////////////////////////////////////////////
//
LLMediaBase* LLMediaEngine::getMediaRenderer()
{
	return mMediaRenderer;
}


//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::init()
{
	if( ! isAvailable() )
		return FALSE;

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::update()
{
	BOOL res = FALSE;
	if( mMediaRenderer )
	{
		S32 result = mMediaRenderer->updateMedia();
		switch(result)
		{
		  case LLMediaBase::updateMediaNeedsSizeChange:
			// Media renderer is requesting a size change.
			handleSizeChangedRequest();
			res = TRUE; // Need to update size of texture
			break;
			
		  case LLMediaBase::updateMediaNeedsUpdate:
			res = TRUE;
			break;
			
		  case LLMediaBase::updateMediaNoChanges:
		  default:
			res = FALSE;
			break;
		}
	}
	return res;
}


//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::load ( const LLString& urlIn, bool web_url, const LLString& path, S32 width_pixels, S32 height_pixels )
{
	if( ! isAvailable() )
		return FALSE;

	if( isLoaded() )
		return TRUE;

	this->unload();

	mMediaRenderer = LLMediaBase::make( LLMediaBase::QuickTime, width_pixels, height_pixels);

	if( ! mMediaRenderer )
		return FALSE;

	if( ! mMediaRenderer->init() )
	{
		delete mMediaRenderer;
		mMediaRenderer = 0;
		return FALSE;
	}

	// do this here since there is no media renderer when we get the update so we store and use here
	if( mMediaRenderer )
		mMediaRenderer->setAutoScaled( mAutoScaled );

	if( ! mMediaRenderer->load( urlIn ) )
	{
		delete mMediaRenderer;
		mMediaRenderer = 0;
		return FALSE;
	}

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::isLoaded()
{
	if( ! isAvailable() )
		return FALSE;

	if( mMediaRenderer )
		return mMediaRenderer->isLoaded();
	else
		return FALSE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::unload()
{
	if( ! isAvailable() )
		return FALSE;

	if( mMediaRenderer )
	{
		mMediaRenderer->stop();
		mMediaRenderer->unload();
		delete mMediaRenderer;
		mMediaRenderer = 0;
		// Don't do this here.  load() calls unload(), and things get lost.
//		mUrl.clear();
//		mImageUUID  = LLUUID::null;
		return TRUE;
	};

	return FALSE;
};

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::play()
{
	if( ! isAvailable() )
		return FALSE;

	// base movie volume on slider in prefs (currently prefs also sets volume directly but other controls
	// may eventually control volume and updat ethis variable
	this->setVolume( mVolume );

	if( mMediaRenderer )
		if( ! mMediaRenderer->play() )
			return FALSE;

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::loop()
{
	if( ! isAvailable() )
		return FALSE;

	// base movie volume on slider in prefs (currently prefs also sets volume directly but other controls
	// may eventually control volume and updat ethis variable
	this->setVolume( mVolume );

	if( mMediaRenderer )
		if( ! mMediaRenderer->loop( 0 ) )
			return FALSE;

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::pause()
{
	if( ! isAvailable() )
		return FALSE;

	if( mMediaRenderer )
		if( ! mMediaRenderer->pause() )
			return FALSE;

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::stop()
{
	if( ! isAvailable() )
		return FALSE;

	if( mMediaRenderer )
		if( ! mMediaRenderer->stop() )
			return FALSE;

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::seek (F64 time)
{
	if( ! isAvailable() )
		return FALSE;

	if( mMediaRenderer )
		if( ! mMediaRenderer->seek (time) )
			return FALSE;

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
void  LLMediaEngine::setAvailable( BOOL availableIn )
{
	mAvailable = availableIn;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::isAvailable()
{
	return mAvailable;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::setVolume( F32 volumeIn )
{
	if( ! isAvailable() )
		return FALSE;

	mVolume = volumeIn;
	
	if( mMediaRenderer )
	{
		if( ! mMediaRenderer->setVolume( volumeIn ) )
		{
			return FALSE;
		};
	};

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaEngine::setEnabled( BOOL enabledIn )
{
	if( mAvailable )
		mEnabled = enabledIn;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::isEnabled()
{
	if( mAvailable )
		return mEnabled;
	else
		return FALSE;
}

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaEngine::setAutoScaled( BOOL autoScaledIn )
{
	mAutoScaled = autoScaledIn;
}

//////////////////////////////////////////////////////////////////////////////
//
BOOL LLMediaEngine::isAutoScaled()
{
	return mAutoScaled;
}

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaEngine::setUrl( const LLString& urlIn )
{
	mUrl = urlIn;
};

//////////////////////////////////////////////////////////////////////////////
//
const LLString& LLMediaEngine::getUrl ()
{
	return mUrl;
};

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaEngine::setImageUUID( LLUUID imageUUIDIn )
{
	mImageUUID = imageUUIDIn;
};

//////////////////////////////////////////////////////////////////////////////
//
LLUUID LLMediaEngine::getImageUUID()
{
	return mImageUUID;
};

//////////////////////////////////////////////////////////////////////////////
//
void LLMediaEngine::handleSizeChangedRequest()
{
	if( ! isAvailable() )
		return;

	// create / resize Raw image
	LLMediaEngine::getInstance()->createImageRaw();

	// tell media library to use this buffer instead of it's own
	if (mImageRaw.notNull())
	{
		LLMediaEngine::getInstance()->getMediaRenderer()->setBuffer( mImageRaw->getData() );
	}

}

//////////////////////////////////////////////////////////////////////////////////////////
// static
void LLMediaEngine::convertImageAndLoadUrl( bool enableLooping, bool web_url, const std::string& path)
{
	LLMediaEngine* engine = LLMediaEngine::getInstance();
	LLString url = engine->getUrl();
	S32 width_pixels = 512;
	S32 height_pixels = 256;
	if (web_url)
	{
		width_pixels = 512;
		height_pixels = 512;
	}

	bool success = false;
	if (engine->load( url, web_url, path, width_pixels, height_pixels ) )
	{
		// create / resize Raw image
		engine->createImageRaw();
		
		// tell media library to use this buffer instead of it's own
		if (engine->getImageRaw())
		{
			engine->getMediaRenderer()->setBuffer( engine->mImageRaw->getData() );
			engine->getMediaRenderer()->setBufferSize(width_pixels, height_pixels);
		
			// start it playing or looping
			if( enableLooping )
			{
				engine->loop();
			}
			else
			{
				engine->play();
			}
			success = true;
		}
	}

	if (!success)
	{
		llinfos << "MEDIA> unable to load " << LLMediaEngine::getInstance()->getUrl() << llendl;
		//LLMediaEngine::getInstance()->setAvailable( FALSE );
	}
}

//////////////////////////////////////////////////////////////////////////////////////////
// static
void LLMediaEngine::process_parcel_media( LLMessageSystem *msg, void ** )
{
	// extract the agent id
	//	LLUUID agent_id;
	//	msg->getUUID( agent_id );

	U32 flags;
	U32 command;
	F32 time;
	msg->getU32( "CommandBlock", "Flags", flags );
	msg->getU32( "CommandBlock", "Command", command);
	msg->getF32( "CommandBlock", "Time", time );

	if (flags &( (1<<PARCEL_MEDIA_COMMAND_STOP) 
				| (1<<PARCEL_MEDIA_COMMAND_PAUSE) 
				| (1<<PARCEL_MEDIA_COMMAND_PLAY) 
				| (1<<PARCEL_MEDIA_COMMAND_LOOP) 
				| (1<<PARCEL_MEDIA_COMMAND_UNLOAD) ))
	{
		// stop
		if( command == PARCEL_MEDIA_COMMAND_STOP )
		{
			//llinfos << ">>> LLMediaEngine::process_parcel_media with command = " <<( '0' + command ) << llendl;

			LLMediaEngine::getInstance()->stop();
		}
		else
		// pause
		if( command == PARCEL_MEDIA_COMMAND_PAUSE )
		{
			//llinfos << ">>> LLMediaEngine::process_parcel_media with command = " <<( '0' + command ) << llendl;

			LLMediaEngine::getInstance()->pause();
		}
		else
		// play
		if( command == PARCEL_MEDIA_COMMAND_PLAY )
		{
			//llinfos << ">>> LLMediaEngine::process_parcel_media with command = " <<( '0' + command ) << llendl;

			convertImageAndLoadUrl( false, false, std::string() );
		}
		else
		// loop
		if( command == PARCEL_MEDIA_COMMAND_LOOP )
		{
			//llinfos << ">>> LLMediaEngine::process_parcel_media with command = " <<( '0' + command ) << llendl;

			// huh? what is play?
			//convertImageAndLoadUrl( play );
			convertImageAndLoadUrl( true, false, std::string() );
		}
		else
		// unload
		if( command == PARCEL_MEDIA_COMMAND_UNLOAD )
		{
			//llinfos << ">>> LLMediaEngine::process_parcel_media with command = " <<( '0' + command ) << llendl;

			if (LLMediaEngine::getInstance()->isLoaded())
			{
				LLMediaEngine::getInstance()->unload();
				LLMediaEngine::getInstance()->destroyImageRaw();
			}
		}
	}

	if (flags & (1<<PARCEL_MEDIA_COMMAND_TIME))
	{
		// do something about fast forward
		LLMediaEngine::getInstance()->seek(time);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////
// static
/*
// ParcelMediaUpdate
// Sends a parcel media update to a single user
// For global updates use the parcel manager.
{
	ParcelMediaUpdate Low NotTrusted Unencoded
	{
		DataBlock Single
		{	MediaURL		Variable	1	}	// string
		{	MediaID			LLUUID			}
		{	MediaAutoScale	U8				}
	}
}
*/
void LLMediaEngine::process_parcel_media_update( LLMessageSystem *msg, void ** )
{
	LLUUID media_uuid;
	char media_url[255];	/* Flawfinder: ignore */
	U8 auto_align;
	msg->getUUID( "DataBlock", "MediaID", media_uuid );
	msg->getString( "DataBlock", "MediaURL", 255, media_url );
	msg->getU8( "DataBlock", "MediaAutoScale", auto_align );

	LLMediaEngine* media_engine = LLMediaEngine::getInstance();
	LLString  url_string ( media_url );

	auto_align = (auto_align) ? TRUE : FALSE;

	if( !( (media_engine->getUrl() == url_string) 
			&& (media_engine->getImageUUID() == media_uuid) 
			&& (media_engine->isAutoScaled() == auto_align ) ) )
	{
		if (media_engine->isLoaded())
		{
			media_engine->unload();
			media_engine->destroyImageRaw();
		}

		media_engine->setUrl(url_string);
		media_engine->setImageUUID(media_uuid);
		media_engine->setAutoScaled(auto_align);
	}
}

// sets proxy information for any of the media impls that may want to use it
void LLMediaEngine::setNetworkProxy ( BOOL enabledIn, const LLString& addressIn, 
										S32 portIn, S32 socksIn, const LLString& excludeIn )
{
	mProxyEnabled = enabledIn;
	mProxyAddress = addressIn;
	mProxyPort = portIn;
	mProxySocks = socksIn;
	mProxyExlude = excludeIn;
}

// gets proxy information for any of the media impls that may want to use it
void LLMediaEngine::getNetworkProxy ( BOOL& enabledOut, LLString& addressOut,
										S32& portOut, S32& socksOut, LLString& excludeOut )
{
	enabledOut = mProxyEnabled;
	addressOut = mProxyAddress;
	portOut = mProxyPort;
	socksOut = mProxySocks;
	excludeOut = mProxyExlude;
}