Menu

[r13]: / trunk / libs / OSMF / org / osmf / net / NetLoader.as  Maximize  Restore  History

Download this file

414 lines (384 with data), 14.3 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
/*****************************************************
*
* 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.
*
* Contributor(s): Akamai Technologies
*
*****************************************************/
package org.osmf.net
{
import __AS3__.vec.Vector;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.Dictionary;
import org.osmf.events.MediaError;
import org.osmf.events.MediaErrorCodes;
import org.osmf.events.MediaErrorEvent;
import org.osmf.events.NetConnectionFactoryEvent;
import org.osmf.media.MediaResourceBase;
import org.osmf.media.MediaType;
import org.osmf.media.MediaTypeUtil;
import org.osmf.media.URLResource;
import org.osmf.traits.LoadState;
import org.osmf.traits.LoadTrait;
import org.osmf.traits.LoaderBase;
import org.osmf.utils.URL;
/**
* The NetLoader class extends LoaderBase to provide
* loading support to the AudioElement and VideoElement classes.
* <p>Supports both streaming and progressive media resources.
* If the resource URL is RTMP, connects to an RTMP server by invoking a NetConnectionFactoryBase.
* NetConnections may be shared between LoadTrait instances.
* If the resource URL is HTTP, performs a <code>connect(null)</code>
* for progressive downloads.</p>
* The NetLoader supports Flash Media Token Authentication,
* for passing authentication tokens through the NetConnection.
*
* @includeExample NetLoaderExample.as -noswf
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class NetLoader extends LoaderBase
{
/**
* Constructor.
*
* @param factory The NetConnectionFactoryBase instance to use for managing NetConnections.
* If factory is null, a NetConnectionFactory will be created and used. Since the
* NetConnectionFactory class facilitates connection sharing, this is an easy way of
* enabling global sharing, by creating a single NetConnectionFactory instance within
* the player and then handing it to all NetLoader instances.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function NetLoader(factory:NetConnectionFactoryBase=null)
{
super();
netConnectionFactory = factory || new NetConnectionFactory();
netConnectionFactory.addEventListener(NetConnectionFactoryEvent.CREATION_COMPLETE, onCreationComplete);
netConnectionFactory.addEventListener(NetConnectionFactoryEvent.CREATION_ERROR, onCreationError);
}
/**
* @private
*
* The NetLoader returns true for URLResources which support the media and mime-types
* (or file extensions) for streaming audio and streaming or progressive video, or
* implement one of the following schemes: http, https, file, rtmp, rtmpt, rtmps,
* rtmpe or rtmpte.
*
* @param resource The URL of the source media.
* @return Returns <code>true</code> for URLResources which it can load
* @inheritDoc
**/
override public function canHandleResource(resource:MediaResourceBase):Boolean
{
var rt:int = MediaTypeUtil.checkMetadataMatchWithResource(resource, MEDIA_TYPES_SUPPORTED, MIME_TYPES_SUPPORTED);
if (rt != MediaTypeUtil.METADATA_MATCH_UNKNOWN)
{
return rt == MediaTypeUtil.METADATA_MATCH_FOUND;
}
/*
* The rules for URL checking is outlined as below:
*
* If the URL is null or empty, we assume being unable to handle the resource
* If the URL has no protocol, we check for file extensions
* If the URL has protocol, we have to make a distinction between progressive and stream
* If the protocol is progressive (file, http, https), we check for file extension
* If the protocol is stream (the rtmp family), we assume that we can handle the resource
*
* We assume being unable to handle the resource for conditions not mentioned above
*/
var res:URLResource = resource as URLResource;
var extensionPattern:RegExp = new RegExp("\.flv$|\.f4v$|\.mov$|\.mp4$|\.mp4v$|\.m4v$|\.3gp$|\.3gpp2$|\.3g2$", "i");
var url:URL = res != null ? new URL(res.url) : null;
if (url == null || url.rawUrl == null || url.rawUrl.length <= 0)
{
return false;
}
if (url.protocol == "")
{
return extensionPattern.test(url.path);
}
if (NetStreamUtils.isRTMPStream(url.rawUrl))
{
return true;
}
if (url.protocol.search(/file$|http$|https$/i) != -1)
{
return (url.path == null ||
url.path.length <= 0 ||
url.path.indexOf(".") == -1 ||
extensionPattern.test(url.path));
}
return false;
}
/**
*
* The factory function for creating a NetStream.
*
* @param connection The NetConnection to associate with the new NetStream.
* @param resource The resource whose content will be played in the NetStream.
*
* @return A new NetStream associated with the NetConnection.
**/
protected function createNetStream(connection:NetConnection, resource:URLResource):NetStream
{
return new NetStream(connection);
}
/**
* The factory function for creating a NetStreamSwitchManagerBase.
*
* @param connection The NetConnection that's associated with the NetStreamSwitchManagerBase.
* @param netStream The NetStream upon which the NetStreamSwitchManagerBase will operate.
* @param dsResource The resource upon which the NetStreamSwitchManagerBase will operate.
*
* @return The NetStreamSwitchManagerBase for the NetStream, null if multi-bitrate switching
* is not enabled for the NetStream.
**/
protected function createNetStreamSwitchManager(connection:NetConnection, netStream:NetStream, dsResource:DynamicStreamingResource):NetStreamSwitchManagerBase
{
return null;
}
/**
* @private
*
* Subclass stub that can be used to do special processing just upfront
* the loader finishing loading. Also, the overriding method must
* call the updateLoadTrait method at the end.
*
* @param loadTrait
*/
protected function processFinishLoading(loadTrait:NetStreamLoadTrait):void
{
updateLoadTrait(loadTrait, LoadState.READY);
}
/**
* @private
*
* Validates the LoadTrait to verify that this class can in fact load it. Examines the protocol
* associated with the LoadTrait's resource. If the protocol is HTTP, calls the <code>startLoadingHTTP()</code>
* method. If the protocol is RTMP-based, calls the <code>startLoadingRTMP()</code> method. If the URL protocol is invalid,
* dispatches a mediaErroEvent against the LoadTrait and updates the LoadTrait's state to LoadState.LOAD_ERROR.
*
* @param loadTrait LoadTrait requesting this load operation.
* @see org.osmf.traits.LoadTrait
* @see org.osmf.traits.LoadState
* @see org.osmf.events.MediaErrorEvent
* @inheritDoc
**/
override protected function executeLoad(loadTrait:LoadTrait):void
{
updateLoadTrait(loadTrait, LoadState.LOADING);
var url:URL = new URL((loadTrait.resource as URLResource).url);
switch (url.protocol)
{
case PROTOCOL_RTMP:
case PROTOCOL_RTMPS:
case PROTOCOL_RTMPT:
case PROTOCOL_RTMPE:
case PROTOCOL_RTMPTE:
startLoadingRTMP(loadTrait);
break;
case PROTOCOL_HTTP:
case PROTOCOL_HTTPS:
case PROTOCOL_FILE:
case PROTOCOL_EMPTY:
startLoadingHTTP(loadTrait);
break;
default:
updateLoadTrait(loadTrait, LoadState.LOAD_ERROR);
loadTrait.dispatchEvent
( new MediaErrorEvent
( MediaErrorEvent.MEDIA_ERROR
, false
, false
, new MediaError(MediaErrorCodes.URL_SCHEME_INVALID)
)
);
break;
}
}
/**
* @private
*
* Unloads the media after validating the unload operation against the LoadTrait.
* Closes the NetStream defined within the NetStreamLoadTrait object,
* as well as the NetConnection defined within the trait object. Dispatches the
* loadStateChange event with every state change.
*
* @throws IllegalOperationError if the parameter is <code>null</code>.
* @param loadTrait LoadTrait to be unloaded.
* @see org.osmf.loaders.LoaderBase#event:loadStateChange
**/
override protected function executeUnload(loadTrait:LoadTrait):void
{
var netLoadTrait:NetStreamLoadTrait = loadTrait as NetStreamLoadTrait;
updateLoadTrait(loadTrait, LoadState.UNLOADING);
netLoadTrait.netStream.close();
if (netLoadTrait.netConnectionFactory != null)
{
netLoadTrait.netConnectionFactory.closeNetConnection(netLoadTrait.connection);
}
else
{
netLoadTrait.connection.close();
}
updateLoadTrait(loadTrait, LoadState.UNINITIALIZED);
}
/**
* Establishes a new NetStream on the connected NetConnection and signals that loading is complete.
*
* @private
**/
private function finishLoading(connection:NetConnection, loadTrait:LoadTrait, factory:NetConnectionFactoryBase = null):void
{
var netLoadTrait:NetStreamLoadTrait = loadTrait as NetStreamLoadTrait;
if (netLoadTrait != null)
{
netLoadTrait.connection = connection;
var netStream:NetStream = createNetStream(connection, netLoadTrait.resource as URLResource);
netStream.client = new NetClient();
netLoadTrait.netStream = netStream;
netLoadTrait.switchManager = createNetStreamSwitchManager(connection, netStream, netLoadTrait.resource as DynamicStreamingResource);
netLoadTrait.netConnectionFactory = factory;
processFinishLoading(loadTrait as NetStreamLoadTrait);
}
}
/**
* Initiates the process of creating a connected NetConnection
*
* @private
*/
private function startLoadingRTMP(loadTrait:LoadTrait):void
{
addPendingLoad(loadTrait);
netConnectionFactory.create(loadTrait.resource as URLResource);
}
/**
* Called once the NetConnectionFactoryBase has successfully created a NetConnection
*
* @private
*/
private function onCreationComplete(event:NetConnectionFactoryEvent):void
{
finishLoading
( event.netConnection
, findAndRemovePendingLoad(event.resource)
, event.currentTarget as NetConnectionFactoryBase
);
}
/**
* Called once the NetConnectionFactoryBase has failed to create a NetConnection
* TBD - error dispatched at lower level.
*
* @private
*/
private function onCreationError(event:NetConnectionFactoryEvent):void
{
var loadTrait:LoadTrait = findAndRemovePendingLoad(event.resource);
if (loadTrait != null)
{
loadTrait.dispatchEvent(new MediaErrorEvent(MediaErrorEvent.MEDIA_ERROR, false, false, event.mediaError));
updateLoadTrait(loadTrait, LoadState.LOAD_ERROR);
}
}
/**
* Initiates a HTTP connection.
*
* @private
*
*/
private function startLoadingHTTP(loadTrait:LoadTrait):void
{
var connection:NetConnection = new NetConnection();
connection.client = new NetClient();
connection.connect(null);
finishLoading(connection, loadTrait);
}
private function addPendingLoad(loadTrait:LoadTrait):void
{
// It's an edge case, but we don't want to assume that we'll never
// have two LoadTraits that use the same URLResource, so we have to
// maintain an Array.
if (pendingLoads[loadTrait.resource] == null)
{
pendingLoads[loadTrait.resource] = [loadTrait];
}
else
{
pendingLoads[loadTrait.resource].push(loadTrait);
}
}
private function findAndRemovePendingLoad(resource:URLResource):LoadTrait
{
var loadTrait:LoadTrait = null;
var pendingLoadsArray:Array = pendingLoads[resource];
if (pendingLoadsArray != null)
{
if (pendingLoadsArray.length == 1)
{
loadTrait = pendingLoadsArray[0] as LoadTrait;
delete pendingLoads[resource];
}
else
{
for (var i:int = 0; i < pendingLoadsArray.length; i++)
{
loadTrait = pendingLoadsArray[i];
if (loadTrait.resource == resource)
{
pendingLoadsArray.splice(i, 1);
break;
}
}
}
}
return loadTrait;
}
private var netConnectionFactory:NetConnectionFactoryBase;
private var pendingLoads:Dictionary = new Dictionary();
private static const PROTOCOL_RTMP:String = "rtmp";
private static const PROTOCOL_RTMPS:String = "rtmps";
private static const PROTOCOL_RTMPT:String = "rtmpt";
private static const PROTOCOL_RTMPE:String = "rtmpe";
private static const PROTOCOL_RTMPTE:String = "rtmpte";
private static const PROTOCOL_HTTP:String = "http";
private static const PROTOCOL_HTTPS:String = "https";
private static const PROTOCOL_FILE:String = "file";
private static const PROTOCOL_EMPTY:String = "";
private static const MEDIA_TYPES_SUPPORTED:Vector.<String> = Vector.<String>([MediaType.VIDEO]);
private static const MIME_TYPES_SUPPORTED:Vector.<String> = Vector.<String>
([
"video/x-flv",
"video/x-f4v",
"video/mp4",
"video/mp4v-es",
"video/x-m4v",
"video/3gpp",
"video/3gpp2",
"video/quicktime",
]);
}
}
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.