Menu

[r16]: / trunk / libs / OSMF / org / osmf / media / pluginClasses / PluginManager.as  Maximize  Restore  History

Download this file

365 lines (329 with data), 11.8 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
/*****************************************************
*
* 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.pluginClasses
{
import __AS3__.vec.Vector;
import flash.events.EventDispatcher;
import flash.utils.Dictionary;
import org.osmf.events.LoadEvent;
import org.osmf.events.MediaErrorEvent;
import org.osmf.events.MediaFactoryEvent;
import org.osmf.events.PluginManagerEvent;
import org.osmf.media.MediaElement;
import org.osmf.media.MediaFactory;
import org.osmf.media.MediaFactoryItem;
import org.osmf.media.MediaResourceBase;
import org.osmf.media.PluginInfoResource;
import org.osmf.media.URLResource;
import org.osmf.traits.LoadState;
import org.osmf.traits.LoadTrait;
import org.osmf.traits.MediaTraitType;
import org.osmf.utils.OSMFStrings;
import org.osmf.utils.Version;
[ExcludeClass]
/**
* Dispatched when the PluginManager has successfully loaded a plugin.
*
* @eventType org.osmf.events.PluginManagerEvent.PLUGIN_LOAD
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="pluginLoad", type="org.osmf.events.PluginManagerEvent")]
/**
* Dispatched when the PluginManager has failed to load a plugin due to an error.
*
* @eventType org.osmf.events.PluginManagerEvent.PLUGIN_LOAD_ERROR
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="pluginLoadError", type="org.osmf.events.PluginManagerEvent")]
/**
* @private
*
* This class is a manager that provide access to plugin related
* features.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class PluginManager extends EventDispatcher
{
/**
* Constructor.
*
* @param mediaFactory MediaFactory within which the PluginManager will place the
* information from loaded plugins.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function PluginManager(mediaFactory:MediaFactory)
{
super();
_mediaFactory = mediaFactory;
_mediaFactory.addEventListener(MediaFactoryEvent.MEDIA_ELEMENT_CREATE, onMediaElementCreate);
minimumSupportedFrameworkVersion = Version.lastAPICompatibleVersion;
initPluginFactory();
_pluginMap = new Dictionary();
_pluginList = new Vector.<PluginEntry>();
}
/**
* Load a plugin identified by resource. The PluginManager will not reload the plugin
* if it has been loaded. Upon successful loading, a PluginManagerEvent.PLUGIN_LOAD
* event will be dispatched. Otherwise, a PluginManagerEvent.PLUGIN_LOAD_ERROR
* event will be dispatched.
*
* @param resource MediaResourceBase at which the plugin (SWF file or class) is hosted. It is assumed that
* it is sufficient to identify a plugin using the MediaResourceBase.
*
* @throws ArgumentError If resource is null or resource is not URLResource or PluginInfoResource
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function loadPlugin(resource:MediaResourceBase):void
{
if (resource == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
var identifier:Object = getPluginIdentifier(resource);
var pluginEntry:PluginEntry = _pluginMap[identifier] as PluginEntry;
if (pluginEntry != null)
{
dispatchEvent
( new PluginManagerEvent
( PluginManagerEvent.PLUGIN_LOAD
, false
, false
, resource
)
);
}
else
{
var pluginElement:MediaElement = _pluginFactory.createMediaElement(resource);
if (pluginElement != null)
{
pluginEntry = new PluginEntry(pluginElement, PluginLoadingState.LOADING);
_pluginMap[identifier] = pluginEntry;
var loadTrait:LoadTrait = pluginElement.getTrait(MediaTraitType.LOAD) as LoadTrait;
if (loadTrait != null)
{
loadTrait.addEventListener(
LoadEvent.LOAD_STATE_CHANGE, onLoadStateChange);
loadTrait.addEventListener(MediaErrorEvent.MEDIA_ERROR, onMediaError);
loadTrait.load();
}
else
{
dispatchEvent(new PluginManagerEvent(PluginManagerEvent.PLUGIN_LOAD_ERROR, false, false, resource));
}
}
else
{
dispatchEvent(new PluginManagerEvent(PluginManagerEvent.PLUGIN_LOAD_ERROR, false, false, resource));
}
}
function onLoadStateChange(event:LoadEvent):void
{
if (event.loadState == LoadState.READY)
{
pluginEntry.state = PluginLoadingState.LOADED;
_pluginList.push(pluginEntry);
var pluginLoadTrait:PluginLoadTrait = pluginElement.getTrait(MediaTraitType.LOAD) as PluginLoadTrait;
if (pluginLoadTrait.pluginInfo.mediaElementCreationNotificationFunction != null)
{
// Inform the newly added plugin about all previously created
// MediaElements.
invokeMediaElementCreationNotificationForCreatedMediaElements(pluginLoadTrait.pluginInfo.mediaElementCreationNotificationFunction);
// Add our notification function to the list of functions to
// call for future-created MediaElements.
if (notificationFunctions == null)
{
notificationFunctions = new Vector.<Function>();
}
notificationFunctions.push(pluginLoadTrait.pluginInfo.mediaElementCreationNotificationFunction);
}
dispatchEvent
( new PluginManagerEvent
( PluginManagerEvent.PLUGIN_LOAD
, false
, false
, resource
)
);
}
else if (event.loadState == LoadState.LOAD_ERROR)
{
// Remove from the pluginMap when the load failed!!!!
delete _pluginMap[identifier];
dispatchEvent(new PluginManagerEvent(PluginManagerEvent.PLUGIN_LOAD_ERROR, false, false, resource));
}
}
function onMediaError(event:MediaErrorEvent):void
{
dispatchEvent(event.clone());
}
}
/**
* Get access to the media factory that is used for plugin loading and
* MediaInfo registering. Plugins can use this MediaFactory to create
* other types of MediaElement.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get mediaFactory():MediaFactory
{
return _mediaFactory;
}
// Internals
//
private function getPluginIdentifier(resource:MediaResourceBase):Object
{
var identifier:Object = null;
if (resource is URLResource)
{
identifier = (resource as URLResource).url;
}
else if (resource is PluginInfoResource)
{
identifier = (resource as PluginInfoResource).pluginInfo;
}
return identifier;
}
private function initPluginFactory():void
{
_pluginFactory = new MediaFactory();
staticPluginLoader = new StaticPluginLoader(mediaFactory, minimumSupportedFrameworkVersion);
dynamicPluginLoader = new DynamicPluginLoader(mediaFactory, minimumSupportedFrameworkVersion);
// Add MediaInfo objects for the static and dynamic plugin loaders.
//
var staticPluginItem:MediaFactoryItem = new MediaFactoryItem
( STATIC_PLUGIN_MEDIA_INFO_ID
, staticPluginLoader.canHandleResource
, createStaticPluginElement
);
_pluginFactory.addItem(staticPluginItem);
var dynamicPluginItem:MediaFactoryItem = new MediaFactoryItem
( DYNAMIC_PLUGIN_MEDIA_INFO_ID
, dynamicPluginLoader.canHandleResource
, createDynamicPluginElement
);
_pluginFactory.addItem(dynamicPluginItem);
}
private function createStaticPluginElement():MediaElement
{
return new PluginElement(staticPluginLoader);
}
private function createDynamicPluginElement():MediaElement
{
return new PluginElement(dynamicPluginLoader);
}
private function onMediaElementCreate(event:MediaFactoryEvent):void
{
// Inform any plugins that need to know about newly-created
// MediaElements about this one.
invokeMediaElementCreationNotifications(event.mediaElement);
// Add the newly created MediaElement to our list of created
// elements, so that it can be passed to the creation notification
// function for any subsequently added plugins. (Note that we
// store it as the key only, so that it will be GC'd if this is
// the only object that holds a reference to it. We set the
// value to an arbitrary Boolean.)
if (createdElements == null)
{
createdElements = new Dictionary(true);
}
createdElements[event.mediaElement] = true;
}
/**
* Invokes the callback for all stored notification functions, for the given
* MediaElement.
**/
private function invokeMediaElementCreationNotifications(mediaElement:MediaElement):void
{
for each (var func:Function in notificationFunctions)
{
invokeMediaElementCreationNotificationFunction(func, mediaElement);
}
}
private function invokeMediaElementCreationNotificationFunction(func:Function, mediaElement:MediaElement):void
{
try
{
func.call(null, mediaElement);
}
catch (error:Error)
{
// Swallow, the notification function is wrongly
// specified. We'll continue as-is.
}
}
/**
* Invokes the creation callback on the given MediaFactoryItem, for
* all created MediaElements.
**/
private function invokeMediaElementCreationNotificationForCreatedMediaElements(func:Function):void
{
// Remember, the MediaElements are stored as the keys (so
// that they can be GC'd if the Dictionary holds the only
// reference), hence we need to do a for..in.
for (var elem:Object in createdElements)
{
invokeMediaElementCreationNotificationFunction(func, elem as MediaElement);
}
}
private var _mediaFactory:MediaFactory;
private var _pluginFactory:MediaFactory;
private var _pluginMap:Dictionary;
private var _pluginList:Vector.<PluginEntry>;
private var notificationFunctions:Vector.<Function>;
private var createdElements:Dictionary;
// Keys are: MediaElement
// Values are: Boolean (just a placeholder, the important part is the key)
private var minimumSupportedFrameworkVersion:String;
private var staticPluginLoader:StaticPluginLoader;
private var dynamicPluginLoader:DynamicPluginLoader;
private static const STATIC_PLUGIN_MEDIA_INFO_ID:String = "org.osmf.plugins.StaticPluginLoader";
private static const DYNAMIC_PLUGIN_MEDIA_INFO_ID:String = "org.osmf.plugins.DynamicPluginLoader";
}
}
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.