Menu

[r15]: / trunk / libs / OSMF / org / osmf / media / MediaFactory.as  Maximize  Restore  History

Download this file

549 lines (493 with data), 16.6 kB

  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
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.media
{
import __AS3__.vec.Vector;
import flash.events.EventDispatcher;
import flash.utils.Dictionary;
import org.osmf.elements.ProxyElement;
import org.osmf.events.MediaFactoryEvent;
import org.osmf.events.PluginManagerEvent;
import org.osmf.media.pluginClasses.PluginManager;
import org.osmf.utils.OSMFStrings;
/**
* Dispatched when the MediaFactory has successfully loaded a plugin.
*
* @eventType org.osmf.events.MediaFactoryEvent.PLUGIN_LOAD
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="pluginLoad", type="org.osmf.events.MediaFactoryEvent")]
/**
* Dispatched when the MediaFactory has failed to load a plugin due to an error.
*
* @eventType org.osmf.events.MediaFactoryEvent.PLUGIN_LOAD_ERROR
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="pluginLoadError", type="org.osmf.events.MediaFactoryEvent")]
/**
* Dispatched when the MediaFactory has created a MediaElement.
*
* @eventType org.osmf.events.MediaFactoryEvent.MEDIA_ELEMENT_CREATE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="mediaElementCreate", type="org.osmf.events.MediaFactoryEvent")]
/**
* MediaFactory represents a factory class for media elements.
*
* <p>The factory operation takes a MediaResourceBase as input and produces a MediaElement
* as output.</p>
* <p>The MediaFactory maintains a list of MediaFactoryItem objects,
* each of which encapsulates all the information necessary to create
* a specific MediaElement. The MediaFactory relies on
* the canHandleResourceFunction method of each MediaFactoryItem to find a
* MediaFactoryItem object that can handle the specified MediaResourceBase.</p>
*
* <p>The factory interface also exposes methods for querying for specific MediaFactoryItem
* objects, and for loading plugins (which hold MediaFactoryItem objects).</p>
*
* @see MediaFactoryItem
* @see MediaResourceBase
* @see MediaElement
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*
* @includeExample MediaFactoryExample.as -noswf
*/
public class MediaFactory extends EventDispatcher
{
/**
* Constructor.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function MediaFactory()
{
super();
allItems = new Dictionary();
}
/**
* Adds the specified MediaFactoryItem to the factory.
* After the MediaFactoryItem has been added, for any MediaResourceBase
* that this MediaFactoryItem can handle, the factory will be able to create
* the corresponding media element.
*
* If a MediaFactoryItem with the same ID already exists in this
* factory, the new MediaFactoryItem object replaces it.
*
* @param item The MediaFactoryItem to add.
*
* @throws ArgumentError If the argument is <code>null</code> or if the argument
* has a <code>null</code> ID field.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function addItem(item:MediaFactoryItem):void
{
if (item == null || item.id == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
var items:Vector.<MediaFactoryItem> = findOrCreateItems(item.type);
// Make sure to overwrite any duplicate.
var existingIndex:int = getIndexOfItem(item.id, items);
if (existingIndex != -1)
{
items[existingIndex] = item;
}
else
{
items.push(item);
}
}
/**
* Removes the specified MediaFactoryItem from the factory.
*
* If no such MediaFactoryItem exists in this factory, does nothing.
*
* @param item The MediaFactoryItem to remove.
*
* @throws ArgumentError If the argument is <code>null</code> or if the argument
* has a <code>null</code> ID field.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function removeItem(item:MediaFactoryItem):void
{
if (item == null || item.id == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
var items:Vector.<MediaFactoryItem> = allItems[item.type];
if (items != null)
{
var existingIndex:int = items.indexOf(item);
if (existingIndex != -1)
{
items.splice(existingIndex, 1);
}
}
}
/**
* The number of MediaFactoryItems managed by the factory.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get numItems():int
{
var numItems:int = 0;
for each (var type:String in MediaFactoryItemType.ALL_TYPES)
{
var items:Vector.<MediaFactoryItem> = allItems[type];
if (items != null)
{
numItems += items.length;
}
}
return numItems;
}
/**
* Gets the MediaFactoryItem at the specified index.
*
* @param index The index in the list from which to retrieve the MediaFactoryItem.
*
* @return The MediaFactoryItem at that index or <code>null</code> if there is none.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getItemAt(index:int):MediaFactoryItem
{
var result:MediaFactoryItem = null;
if (index >= 0)
{
for each (var type:String in MediaFactoryItemType.ALL_TYPES)
{
var items:Vector.<MediaFactoryItem> = allItems[type];
if (items != null)
{
if (index < items.length)
{
result = items[index];
break;
}
else
{
// Not in this list, try the next.
index -= items.length;
}
}
}
}
return result;
}
/**
* Returns the MediaFactoryItem with the specified ID or <code>null</code> if the
* specified MediaFactoryItem does not exist in this factory.
*
* @param The ID of the MediaFactoryItem to retrieve.
*
* @return The MediaFactoryItem with the specified ID or <code>null</code> if the specified
* MediaFactoryItem does not exist in this factory.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getItemById(id:String):MediaFactoryItem
{
var result:MediaFactoryItem = null;
for each (var type:String in MediaFactoryItemType.ALL_TYPES)
{
var items:Vector.<MediaFactoryItem> = allItems[type];
if (items != null)
{
var existingIndex:int = getIndexOfItem(id, items);
if (existingIndex != -1)
{
result = items[existingIndex];
break;
}
}
}
return result;
}
/**
* Load a plugin identified by the specified resource. The MediaFactory will not
* reload the plugin if it has already been loaded. Upon successful loading, the
* MediaFactoryItems within the plugin's PluginInfo property will be added to
* this MediaFactory, and a MediaFactoryEvent.PLUGIN_LOAD event will be dispatched.
* OtherwiseIf the load fails, a MediaFactoryEvent.PLUGIN_LOAD_ERROR event will be
* dispatched.
*
* @param resource MediaResourceBase representing the plugin. For remote (dynamic)
* plugins, use an URLResource pointing to the remote SWF to load. For local
* (static) plugins, use a PluginInfoResource.
*
* @throws ArgumentError If resource is null or resource is neither an URLResource
* nor a PluginInfoResource.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function loadPlugin(resource:MediaResourceBase):void
{
createPluginManager();
pluginManager.loadPlugin(resource);
}
/**
* Returns a MediaElement that can be created based on the specified
* MediaResourceBase.
* <p>Returns <code>null</code> if the factory cannot
* find a MediaFactoryItem object
* capable of creating such a MediaElement in this factory.</p>
*
* @param resource The MediaResourceBase for which a corresponding
* MediaElement should be created.
*
* @return The MediaElement that was created or <code>null</code> if no such
* MediaElement could be created from the resource.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function createMediaElement(resource:MediaResourceBase):MediaElement
{
// Make sure we have a plugin manager before creating a MediaElement, so
// that it will catch the mediaElementCreate event.
createPluginManager();
// Note that proxies are resolved before creation callbacks are called:
// if a media element is proxied, then the creation callback will be invoked
// with the root proxy, not the wrapped media element.
//
// We attempt to create a MediaElement of the STANDARD type.
var mediaElement:MediaElement = createMediaElementByResource(resource, MediaFactoryItemType.STANDARD);
if (mediaElement != null)
{
var proxyElement:MediaElement =
createMediaElementByResource
( mediaElement.resource
, MediaFactoryItemType.PROXY
, mediaElement /* element to wrap */
);
// If we have a corresponding ProxyElement, then instead of
// returning the STANDARD MediaElement, we instead return that
// PROXY element as the wrapper for the STANDARD element.
mediaElement = (proxyElement != null ? proxyElement : mediaElement);
// Inform clients of the creation.
dispatchEvent
( new MediaFactoryEvent
( MediaFactoryEvent.MEDIA_ELEMENT_CREATE
, false
, false
, null
, mediaElement
)
);
}
return mediaElement;
}
// Protected
//
/**
* Returns the most appropriate MediaFactoryItem for the specified resource
* out of the MediaFactoryItems in the specified list.
*
* This method is invoked when <code>createMediaElement</code> is invoked
* with a resource that more than one MediaFactoryItem can handle. Subclasses
* can override to select the most appropriate one.
*
* The default behavior is to select the first item which is not "native" to
* the framework, under the theory that plugins ought to take precedence over
* core media types. It makes this decision based on the presence or absence
* of an id value which starts with "org.osmf".
*/
protected function resolveItems(resource:MediaResourceBase, items:Vector.<MediaFactoryItem>):MediaFactoryItem
{
if (resource == null || items == null)
{
return null;
}
var firstNativeItem:MediaFactoryItem = null;
for (var i:int = 0; i < items.length; i++)
{
var item:MediaFactoryItem = items[i] as MediaFactoryItem;
if (item.id.indexOf("org.osmf") == -1)
{
// Non-native, we'll take it.
return item;
}
else if (firstNativeItem == null)
{
firstNativeItem = item;
}
}
return firstNativeItem;
}
// Internals
//
private function findOrCreateItems(type:String):Vector.<MediaFactoryItem>
{
if (allItems[type] == null)
{
allItems[type] = new Vector.<MediaFactoryItem>();
}
return allItems[type] as Vector.<MediaFactoryItem>;
}
private function createMediaElementByResource
( resource:MediaResourceBase
, itemType:String
, wrappedElement:MediaElement=null
):MediaElement
{
var mediaElement:MediaElement = null;
var items:Vector.<MediaFactoryItem> = getItemsByResource(resource, allItems[itemType]);
if (itemType == MediaFactoryItemType.STANDARD)
{
var item:MediaFactoryItem = resolveItems(resource, items) as MediaFactoryItem;
if (item != null)
{
mediaElement = invokeMediaElementCreationFunction(item);
}
}
else if (itemType == MediaFactoryItemType.PROXY)
{
var nextElementToWrap:MediaElement = wrappedElement;
// Create our chain of proxies, starting from the bottom so
// that we can assign the base wrapped element. (Note that
// we iterate from the end to the beginning simply to make
// it easier to assign the wrappedElement in our for loop.
// In the future, we may want to provide control for the
// ordering to the client through some type of resolver method.
for (var i:int = items.length; i > 0; i--)
{
var proxyItem:MediaFactoryItem = items[i-1] as MediaFactoryItem;
var proxyElement:ProxyElement = invokeMediaElementCreationFunction(proxyItem) as ProxyElement;
if (proxyElement != null)
{
proxyElement.proxiedElement = nextElementToWrap;
nextElementToWrap = proxyElement;
}
}
mediaElement = nextElementToWrap;
}
if (mediaElement != null)
{
mediaElement.resource = resource;
}
return mediaElement;
}
private static function getItemsByResource(resource:MediaResourceBase, items:Vector.<MediaFactoryItem>):Vector.<MediaFactoryItem>
{
var results:Vector.<MediaFactoryItem> = new Vector.<MediaFactoryItem>();
for each (var item:MediaFactoryItem in items)
{
if (item.canHandleResourceFunction(resource))
{
results.push(item);
}
}
return results;
}
private static function getIndexOfItem(id:String, items:Vector.<MediaFactoryItem>):int
{
for (var i:int = 0; i < items.length; i++)
{
var item:MediaFactoryItem = items[i] as MediaFactoryItem;
if (item.id == id)
{
return i;
}
}
return -1;
}
private function onPluginLoad(event:PluginManagerEvent):void
{
dispatchEvent(new MediaFactoryEvent(MediaFactoryEvent.PLUGIN_LOAD, false, false, event.resource));
}
private function onPluginLoadError(event:PluginManagerEvent):void
{
dispatchEvent(new MediaFactoryEvent(MediaFactoryEvent.PLUGIN_LOAD_ERROR, false, false, event.resource));
}
private function invokeMediaElementCreationFunction(item:MediaFactoryItem):MediaElement
{
var mediaElement:MediaElement = null;
try
{
mediaElement = item.mediaElementCreationFunction();
}
catch (error:Error)
{
// Swallow, the creation function is wrongly specified.
// We'll just return a null MediaElement.
}
return mediaElement;
}
private function createPluginManager():void
{
if (pluginManager == null)
{
pluginManager = new PluginManager(this);
pluginManager.addEventListener(PluginManagerEvent.PLUGIN_LOAD, onPluginLoad);
pluginManager.addEventListener(PluginManagerEvent.PLUGIN_LOAD_ERROR, onPluginLoadError);
}
}
private var pluginManager:PluginManager;
private var allItems:Dictionary;
// Keys are: String (MediaFactoryItemType)
// Values are: Vector.<MediaFactoryItem>
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.