Menu

[r331]: / trunk / framework / OSMF / org / osmf / net / FMSURL.as  Maximize  Restore  History

Download this file

460 lines (414 with data), 13.7 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
/*****************************************************
*
* 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 org.osmf.utils.URL;
[ExcludeClass]
/**
* @private
*
* Parses a URL into properties specific to Flash Media Server.
*
* @see URL
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class FMSURL extends URL
{
/**
* Set the URL this class will work with.
*
* @param url The URL this class will use to provide FMS-specific information such as app name and instance name.
* @param useInstance If true, then the second part of the URL path is considered the instance name,
* such as <code>rtmp://host/app/foo/bar/stream</code>. In this case the instance name would be 'foo' and the stream would
* be 'bar/stream'.
* If false, then the second part of the URL path is considered to be the stream name,
* such as <code>rtmp://host/app/foo/bar/stream</code>. In this case there is no instance name and the stream would
* be 'foo/bar/stream'.
*
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function FMSURL(url:String, useInstance:Boolean=false)
{
super(url);
_useInstance = useInstance;
_appName = "";
_instanceName = "";
_streamName = "";
_fileFormat = "";
parsePath();
parseQuery();
}
/**
* Whether a named instance is being used within the URI
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get useInstance():Boolean
{
return _useInstance;
}
/**
* The FMS application name.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get appName():String
{
return _appName;
}
/**
* The FMS instance name.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get instanceName():String
{
return _instanceName;
}
/**
* The FMS stream name.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get streamName():String
{
return _streamName;
}
/**
* The file format of the streaming media. Corresponds to one of the
* public constants defined in this class, such as MP4_STREAM,
* or the blank stream for flv media streams.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get fileFormat():String
{
return _fileFormat;
}
/**
* The vector of edges.
*
* @see FMSHost
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get edges():Vector.<FMSHost>
{
return _edges;
}
/**
* The vector of origins.
*
* @see FMSHost
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get origins():Vector.<FMSHost>
{
return _origins;
}
/**
* Parse the path in the URL object into FMS specific properties.
* The path is everything after the host but before any query string parameters, with no leading or trailing slashes.
* <p>
* For example, in this URL: <code>"http://host.com:80/foo/bar/index.html?a=1&#38;b=2"</code>
* path would be <code>"foo/bar/index.html"</code></p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function parsePath():void
{
if ((path == null) || (path.length == 0))
{
// Check the query string for a stream name since the path is empty
_streamName = getParamValue(QUERY_STRING_STREAM);
// Check the query string for stream type since the path is empty
_fileFormat = getParamValue(QUERY_STRING_STREAMTYPE);
return;
}
var pattern:RegExp = /(\/)/;
var result:Array = path.split(pattern);
if (result != null)
{
_appName = result[APPNAME_START_INDEX];
_instanceName = "";
_streamName = "";
// If "_definst_" is in the path and in the right place, we'll assume everything after that is the stream
var definstPattern:RegExp = new RegExp("^.*\/" + DEFAULT_INSTANCE_NAME, "i");
if (path.search(definstPattern) > -1)
{
_useInstance = true;
}
var streamStartNdx:uint = STREAMNAME_START_INDEX;
if (_useInstance)
{
_instanceName = result[INSTANCENAME_START_INDEX];
}
else
{
streamStartNdx = INSTANCENAME_START_INDEX;
}
for (var i:int = streamStartNdx; i < result.length; i++)
{
_streamName += result[i];
}
// If no streamName found in the path, check the query string
if (_streamName == null || _streamName == "")
{
_streamName = getParamValue(QUERY_STRING_STREAM);
}
if (_streamName.search(/^mp4:/i) > -1)
{
_fileFormat = MP4_STREAM;
}
else if (_streamName.search(/^mp3:/i) > -1)
{
_fileFormat = MP3_STREAM;
}
else if (_streamName.search(/^id3:/i) > -1)
{
_fileFormat = ID3_STREAM;
}
// If no stream type found check the query string
if (_fileFormat == null || _fileFormat == "")
{
_fileFormat = getParamValue(QUERY_STRING_STREAMTYPE);
}
}
/*
* At this point, the parser may have parsed the FMS URL incorrectly. The following
* is a detailed description:
*
* RTMP URL has the format as this: RTMP://server<:port>/applicationName/<applicationInstance>/streamName
*
* where port and applicationInstance are optional.
*
* To make matters more complicated, applicationName and applicationInstance can contain slashes.
* For instance, applicationName_part1/applicationName_part2/applicationName_part3.
* Since there is no hint about the boundary between applicationName and applicationInstance, a string
* such as string_segment_1/string_segment_2/string_segment_3 is to up arbitrary interpretations.
*
* The issue depicted above should not matter too much since both applicationName and applicationInstance
* are part of the RTMP connection string. However, it becomes an issue because a erroneous parsing
* will lead to a malformatted streamName.
*
* For instance we have the following URL:
* rtmp://server/applicationName_part1/applicationName_part2/applicationInstance/mp4:video.f4v
*
* Up to this point, the parse will consider:
* applicationName = "applicationName_part1"
* applicationInstance = "applicationName_part2"
* streamName = "applicationInstance/mp4:video.f4v"
*
* mp4, as well as mp3 and id3, is the prefix of streamName. These prefixes can provide hints
* of where the streamName starts. As a result, whatever in front of the prefix should be considered
* part of applicationInstance. One may argument that the portion of the streamName might technically be
* part of the applicationName if applicationInstance is absent. The argument is right, but again there
* is not enough information to make a perfect decision. Furthermore, as mentioned before,
* it is more important to figure out the port of the streamName actually belongs to the connection string
* rather than pin point which part of the connection string.
*
* The following code adjusts the erroneously parsed streamName as described above. One caveat is that
* this adjustment of parsing will fail when dealing with flv files since there is no prefix for it.
*/
var mp4PrefixStart:int = _streamName.indexOf("/mp4:");
var mp3PrefixStart:int = _streamName.indexOf("/mp3:");
var id3PrefixStart:int = _streamName.indexOf("/id3:");
var prefixStart:int = -1;
if (mp4PrefixStart > 0)
{
prefixStart = mp4PrefixStart;
}
else if (mp3PrefixStart > 0)
{
prefixStart = mp3PrefixStart;
}
else if (id3PrefixStart > 0)
{
prefixStart = id3PrefixStart;
}
if (useInstance && prefixStart > 0)
{
_instanceName += "/";
_instanceName += _streamName.substr(0, prefixStart);
_streamName = streamName.substr(prefixStart + 1);
}
}
/**
* Parse the query string for origin/edge info.
* A sample FMS URI with origin/edge info in the query string might look like this:
* "rtmp://edge1/?rtmp://edge2/?rtmp://origin/app/inst/mp4:foldera/folder/b/myfile.mp4"
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function parseQuery():void
{
// If there is no query string or there are no protocols in the query string, there is nothing to do
if (query == null || query.length == 0 || (query.search(/:\//) == -1))
{
return;
}
var edgeOriginURIs:Array = query.split("?");
// Remove the items that don't have a protocol
for (var ndx:int = 0; ndx < edgeOriginURIs.length; ndx++)
{
var tempIndex:int = edgeOriginURIs[ndx].toString().search(/:\//);
if (tempIndex == -1)
{
edgeOriginURIs.splice(ndx, 1);
}
}
var hasEdge:Boolean = false;
var originIndex:int = 0;
// if it splits into more than one item, we assume it has an edge and the last one is the origin
if (edgeOriginURIs.length >= 2) {
hasEdge = true;
originIndex = edgeOriginURIs.length -1;
}
var tempSN:String = ""; // temporary server name
var tempPN:String = ""; // temporary port number
var colonIndex:int = 0;
var slashIndex:int = 0;
var startIndex:int = 0;
var endIndex:int = 0;
for (var i:int = 0; i < edgeOriginURIs.length; i++)
{
var tempNdex:int = edgeOriginURIs[i].toString().search(/:\//);
startIndex = tempNdex + 2;
if (edgeOriginURIs[i].charAt(startIndex) == '/')
{
// if not local URI (i.e. rtmp:/app/) then move index up
startIndex++;
}
// get server (and maybe port)
colonIndex = edgeOriginURIs[i].indexOf(":", startIndex);
slashIndex = edgeOriginURIs[i].indexOf("/", startIndex);
if (slashIndex < 0 && colonIndex < 0)
{
tempSN = edgeOriginURIs[i].slice(startIndex);
}
else if (colonIndex >= 0 && colonIndex < slashIndex)
{
endIndex = colonIndex;
tempSN = edgeOriginURIs[i].slice(startIndex, endIndex);
startIndex = endIndex + 1;
endIndex = slashIndex;
tempPN = edgeOriginURIs[i].slice(startIndex, endIndex);
}
else if (edgeOriginURIs[i].indexOf("://") != -1)
{
endIndex = slashIndex;
tempSN = edgeOriginURIs[i].slice(startIndex, endIndex);
}
else
{
endIndex = edgeOriginURIs[i].indexOf("/");
tempSN = "localhost";
}
// if it's the origin, we need to push the origin and get the app and stream name
if (i == originIndex)
{
if (_origins == null)
{
_origins = new Vector.<FMSHost>;
}
_origins.push(new FMSHost(tempSN, tempPN));
var tempFMSURL:FMSURL = new FMSURL(edgeOriginURIs[i], _useInstance);
if (_appName == "")
{
_appName = tempFMSURL.appName;
}
if (_useInstance && _instanceName == "")
{
_instanceName = tempFMSURL.instanceName;
}
if (_streamName == "")
{
_streamName = tempFMSURL.streamName;
}
}
else if((edgeOriginURIs[i] != query) && hasEdge)
{
if (_edges == null)
{
_edges = new Vector.<FMSHost>;
}
_edges.push(new FMSHost(tempSN, tempPN));
}
}
}
private var _useInstance:Boolean;
private var _appName:String;
private var _instanceName:String;
private var _streamName:String;
private var _fileFormat:String;
private var _origins:Vector.<FMSHost>;
private var _edges:Vector.<FMSHost>;
private static const APPNAME_START_INDEX:uint = 0;
private static const INSTANCENAME_START_INDEX:uint = 2;
private static const STREAMNAME_START_INDEX:uint = 4;
private static const DEFAULT_INSTANCE_NAME:String = "_definst_";
public static const MP4_STREAM:String = "mp4";
public static const MP3_STREAM:String = "mp3";
public static const ID3_STREAM:String = "id3";
public static const QUERY_STRING_STREAM:String = "streamName";
public static const QUERY_STRING_STREAMTYPE:String = "streamType";
}
}
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.