Menu

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

Download this file

578 lines (513 with data), 16.5 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
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
/*****************************************************
*
* 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.net
{
import __AS3__.vec.Vector;
import flash.errors.IllegalOperationError;
import flash.events.NetStatusEvent;
import flash.events.TimerEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamPlayOptions;
import flash.net.NetStreamPlayTransitions;
import flash.utils.Dictionary;
import flash.utils.Timer;
import flash.utils.getTimer;
import org.osmf.utils.OSMFStrings;
CONFIG::LOGGING
{
import org.osmf.logging.Logger;
import org.osmf.logging.Log;
}
/**
* NetStreamSwitchManager is a default implementation of
* NetStreamSwitchManagerBase. It manages transitions between
* multi-bitrate (MBR) streams using configurable switching rules.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
**/
public class NetStreamSwitchManager extends NetStreamSwitchManagerBase
{
/**
* Constructor.
*
* @param connection The NetConnection for the NetStream that will be managed.
* @param netStream The NetStream to manage.
* @param resource The DynamicStreamingResource that is playing in the NetStream.
* @param metrics The provider of runtime metrics.
* @param switchingRules The switching rules that this manager will use.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
**/
public function NetStreamSwitchManager
( connection:NetConnection
, netStream:NetStream
, resource:DynamicStreamingResource
, metrics:NetStreamMetricsBase
, switchingRules:Vector.<SwitchingRuleBase>,
autoSwitch:Boolean = true)
{
super();
this.connection = connection;
this.netStream = netStream;
this.dsResource = resource;
this.metrics = metrics;
metrics.resource = resource;
this.switchingRules = switchingRules || new Vector.<SwitchingRuleBase>();
_currentIndex = Math.max(0, Math.min(maxAllowedIndex, dsResource.initialIndex));
checkRulesTimer = new Timer(RULE_CHECK_INTERVAL);
checkRulesTimer.addEventListener(TimerEvent.TIMER, checkRules);
super.autoSwitch = autoSwitch;
failedDSI = new Dictionary();
initDSIFailedCounts();
// We set the bandwidth in both directions based on a multiplier applied to the bitrate level.
_bandwidthLimit = 1.4 * resource.streamItems[resource.streamItems.length-1].bitrate * 1000/8;
netStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
// Make sure we get onPlayStatus first (by setting a higher priority)
// so that we can expose a consistent state to clients.
NetClient(netStream.client).addHandler(NetStreamCodes.ON_PLAY_STATUS, onPlayStatus, int.MAX_VALUE);
}
/**
* @private
*/
override public function set autoSwitch(value:Boolean):void
{
super.autoSwitch = value;
CONFIG::LOGGING
{
logger.debug("autoSwitch() - setting to " + value);
}
if (_autoSwitch)
{
CONFIG::LOGGING
{
logger.debug("autoSwitch() - starting check rules timer.");
}
checkRulesTimer.start();
}
else
{
CONFIG::LOGGING
{
logger.debug("autoSwitch() - stopping check rules timer.");
}
checkRulesTimer.stop();
}
}
/**
* @private
*/
override public function get currentIndex():uint
{
return _currentIndex;
}
/**
* @private
*/
override public function get maxAllowedIndex():int
{
var count:int = dsResource.streamItems.length - 1;
return (count < super.maxAllowedIndex ? count : super.maxAllowedIndex);
}
/**
* @private
*/
override public function set maxAllowedIndex(value:int):void
{
if (value > dsResource.streamItems.length)
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_INVALID_INDEX));
}
super.maxAllowedIndex = value;
metrics.maxAllowedIndex = value;
}
/**
* @private
**/
override public function switchTo(index:int):void
{
if (!_autoSwitch)
{
if (index < 0 || index > maxAllowedIndex)
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_INVALID_INDEX));
}
else
{
CONFIG::LOGGING
{
logger.debug("switchTo() - manually switching to index: " + index);
}
if (actualIndex == -1)
{
prepareForSwitching();
}
executeSwitch(index);
}
}
else
{
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.STREAMSWITCH_STREAM_NOT_IN_MANUAL_MODE));
}
}
// Protected
//
/**
* Override this method to provide additional decisioning around
* allowing automatic switches to occur. This method will be invoked
* just prior to a switch request. If false is returned, that switch
* request will not take place.
*
* <p>By default, the implementation does the following:</p>
* <p>1) When a switch down occurs, the stream being switched from has its
* failed count incremented. If, when the switching rules are evaluated
* again, a rule suggests switching up, since the stream previously
* failed, it won't be tried again until a duration (30s) elapses. This
* provides a better user experience by preventing a situation where
* the switch up is attempted but then fails almost immediately.</p>
* <p>2) Once a stream item has 3 failures, there will be no more
* attempts to switch to it until an interval (5m) has expired. At the
* end of this interval, all failed counts are reset to zero.</p>
*
* @param newIndex The new index to switch to.
**/
protected function canAutoSwitchNow(newIndex:int):Boolean
{
// If this stream has failed, we don't want to try it again until
// the wait period has elapsed
if (dsiFailedCounts[newIndex] >= 1)
{
var current:int = getTimer();
if (current - failedDSI[newIndex] < DEFAULT_WAIT_DURATION_AFTER_DOWN_SWITCH)
{
CONFIG::LOGGING
{
logger.debug("canAutoSwitchNow() - ignoring switch request because index has " + dsiFailedCounts[newIndex]+" failure(s) and only "+ (current - failedDSI[newIndex])/1000 + " seconds have passed since the last failure.");
}
return false;
}
}
// If the requested index is currently locked out, then we don't
// allow the switch.
else if (dsiFailedCounts[newIndex] > DEFAULT_MAX_UP_SWITCHES_PER_STREAM_ITEM)
{
return false;
}
return true;
}
/**
* The multiplier to apply to the maximum bandwidth for the client. The
* default is 140% of the highest bitrate stream.
**/
protected final function get bandwidthLimit():Number
{
return _bandwidthLimit;
}
protected final function set bandwidthLimit(value:Number):void
{
_bandwidthLimit = value;
}
/**
* Forces the evaluation of all the rules.
*
* @private
**/
protected function doCheckRules():void
{
checkRules(null);
}
// Internals
//
/**
* Executes the switch to the specified index.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function executeSwitch(targetIndex:int):void
{
var nso:NetStreamPlayOptions = new NetStreamPlayOptions();
var playArgs:Object = NetStreamUtils.getPlayArgsForResource(dsResource);
nso.start = playArgs.start;
nso.len = playArgs.len;
nso.streamName = dsResource.streamItems[targetIndex].streamName;
//
// FM-925, it seems that the oldStreamName cannot contain parameters,
// therefore we must remove them
//
var sn:String = oldStreamName;
if (sn != null && sn.indexOf("?") >= 0)
{
nso.oldStreamName = sn.substr(0, sn.indexOf("?"));
}
else
{
nso.oldStreamName = oldStreamName;
}
nso.transition = NetStreamPlayTransitions.SWITCH;
CONFIG::LOGGING
{
logger.debug("executeSwitch() - Switching to index " + (targetIndex) + " at " + Math.round(dsResource.streamItems[targetIndex].bitrate) + " kbps");
}
switching = true;
netStream.play2(nso);
oldStreamName = dsResource.streamItems[targetIndex].streamName;
if (targetIndex < actualIndex && _autoSwitch)
{
// This is a failure for the current stream, so let's tag it as such.
incrementDSIFailedCount(actualIndex);
// Keep track of when it failed so we don't try it again for
// another failedItemWaitPeriod milliseconds to improve the
// user experience.
failedDSI[actualIndex] = getTimer();
}
}
/**
* Checks all the switching rules. If a switching rule returns -1, it is
* recommending no change. If a switching rule returns a number greater than
* -1 it is recommending a switch to that index. This method uses the lesser of
* all the recommended indices that are greater than -1.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
private function checkRules(event:TimerEvent):void
{
if (switchingRules == null || switching || dsResource == null)
{
return;
}
var newIndex:int = int.MAX_VALUE;
for (var i:int = 0; i < switchingRules.length; i++)
{
var n:int = switchingRules[i].getNewIndex();
if (n != -1 && n < newIndex)
{
newIndex = n;
}
}
// if the rules recommended a new bitrate index
// then make sure we respect the maximum allowed index
if ( newIndex != -1
&& newIndex != int.MAX_VALUE
&& newIndex != actualIndex
)
{
newIndex = Math.min(newIndex, maxAllowedIndex);
}
// if no change was recommended then make sure that
// the current index is still respecting the maximum
// allowed index
if (
(newIndex == -1 || newIndex == int.MAX_VALUE)
&& actualIndex > maxAllowedIndex
)
{
newIndex = maxAllowedIndex;
}
if ( newIndex != -1
&& newIndex != int.MAX_VALUE
&& newIndex != actualIndex
&& !switching
&& newIndex <= maxAllowedIndex
)
{
if (actualIndex == -1)
{
prepareForSwitching();
}
if (canAutoSwitchNow(newIndex))
{
CONFIG::LOGGING
{
logger.debug("checkRules() - Calling for switch to " + newIndex + " at " + dsResource.streamItems[newIndex].bitrate + " kbps");
}
executeSwitch(newIndex);
}
}
}
private function onNetStatus(event:NetStatusEvent):void
{
CONFIG::LOGGING
{
logger.debug("onNetStatus() - event.info.code=" + event.info.code);
}
switch (event.info.code)
{
case NetStreamCodes.NETSTREAM_PLAY_START:
if (actualIndex == -1)
{
prepareForSwitching();
}
else if (_autoSwitch && checkRulesTimer.running == false)
{
checkRulesTimer.start();
}
break;
case NetStreamCodes.NETSTREAM_PLAY_TRANSITION:
actualIndex = dsResource.indexFromName(event.info.details);
if (actualIndex > -1)
{
switching = false;
metrics.currentIndex = actualIndex;
lastTransitionIndex = actualIndex;
}
break;
case NetStreamCodes.NETSTREAM_PLAY_FAILED:
switching = false;
break;
case NetStreamCodes.NETSTREAM_SEEK_NOTIFY:
switching = false;
if (lastTransitionIndex >= 0)
{
_currentIndex = lastTransitionIndex;
}
break;
case NetStreamCodes.NETSTREAM_PLAY_STOP:
checkRulesTimer.stop();
CONFIG::LOGGING
{
logger.debug("onNetStatus() - Stopping rules since server has stopped sending data");
}
break;
}
}
private function onPlayStatus(info:Object):void
{
CONFIG::LOGGING
{
logger.debug("onPlayStatus() - info.code=" + info.code);
}
switch (info.code)
{
case NetStreamCodes.NETSTREAM_PLAY_TRANSITION_COMPLETE:
if (lastTransitionIndex >= 0)
{
_currentIndex = lastTransitionIndex;
lastTransitionIndex = -1;
}
CONFIG::LOGGING
{
logger.debug("onPlayStatus() - Transition complete to index: " + currentIndex + " at " + Math.round(dsResource.streamItems[currentIndex].bitrate) + " kbps");
}
break;
}
}
/**
* Prepare the manager for switching. Note that this doesn't necessarily
* mean a switch is imminent.
**/
private function prepareForSwitching():void
{
initDSIFailedCounts();
metrics.resource = dsResource;
actualIndex = 0;
lastTransitionIndex = -1;
if ((dsResource.initialIndex >= 0) && (dsResource.initialIndex < dsResource.streamItems.length))
{
actualIndex = dsResource.initialIndex;
}
if (_autoSwitch)
{
checkRulesTimer.start();
}
setThrottleLimits(dsResource.streamItems.length - 1);
CONFIG::LOGGING
{
logger.debug("prepareForSwitching() - Starting with stream index " + actualIndex + " at " + Math.round(dsResource.streamItems[actualIndex].bitrate) + " kbps");
}
metrics.currentIndex = actualIndex;
}
private function initDSIFailedCounts():void
{
if (dsiFailedCounts != null)
{
dsiFailedCounts.length = 0;
dsiFailedCounts = null;
}
dsiFailedCounts = new Vector.<int>();
for (var i:int = 0; i < dsResource.streamItems.length; i++)
{
dsiFailedCounts.push(0);
}
}
private function incrementDSIFailedCount(index:int):void
{
dsiFailedCounts[index]++;
// Start the timer that clears the failed counts if one of them
// just went over the max failed count
if (dsiFailedCounts[index] > DEFAULT_MAX_UP_SWITCHES_PER_STREAM_ITEM)
{
if (clearFailedCountsTimer == null)
{
clearFailedCountsTimer = new Timer(DEFAULT_CLEAR_FAILED_COUNTS_INTERVAL, 1);
clearFailedCountsTimer.addEventListener(TimerEvent.TIMER, clearFailedCounts);
}
clearFailedCountsTimer.start();
}
}
private function clearFailedCounts(event:TimerEvent):void
{
clearFailedCountsTimer.removeEventListener(TimerEvent.TIMER, clearFailedCounts);
clearFailedCountsTimer = null;
initDSIFailedCounts();
}
private function setThrottleLimits(index:int):void
{
connection.call("setBandwidthLimit", null, _bandwidthLimit, _bandwidthLimit);
}
private var netStream:NetStream;
private var dsResource:DynamicStreamingResource;
private var switchingRules:Vector.<SwitchingRuleBase>;
private var metrics:NetStreamMetricsBase;
private var checkRulesTimer:Timer;
private var clearFailedCountsTimer:Timer;
private var actualIndex:int = -1;
private var oldStreamName:String;
private var switching:Boolean;
private var _currentIndex:int;
private var lastTransitionIndex:int = -1;
private var connection:NetConnection;
private var dsiFailedCounts:Vector.<int>; // This vector keeps track of the number of failures
// for each DynamicStreamingItem in the DynamicStreamingResource
private var failedDSI:Dictionary;
private var _bandwidthLimit:Number = 0;;
private static const RULE_CHECK_INTERVAL:Number = 500; // Switching rule check interval in milliseconds
private static const DEFAULT_MAX_UP_SWITCHES_PER_STREAM_ITEM:int = 3;
private static const DEFAULT_WAIT_DURATION_AFTER_DOWN_SWITCH:int = 30000;
private static const DEFAULT_CLEAR_FAILED_COUNTS_INTERVAL:Number = 300000; // default of 5 minutes for clearing failed counts on stream items
CONFIG::LOGGING
{
private static const logger:Logger = Log.getLogger("org.osmf.net.NetStreamSwitchManager");
}
}
}
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.