Brought up to date with the private repository (feea4c7)

This commit is contained in:
S committed 2026-10-14 20:57:33 +03:00
1 parent 1aa738d692
commit 1b1df1ae48
7925 files changed
+2617237 -20

No files matched your search

@@ -0,0 +1,77 @@
package org.osmf.net.rtmpstreaming
{
import flash.utils.getTimer;
import org.osmf.net.NetStreamMetricsBase;
import org.osmf.net.SwitchingRuleBase;
public class DroppedFramesRule extends SwitchingRuleBase
{
private static const LOCK_INTERVAL:Number = 30000;
private var downSwitchByOne:int;
private var downSwitchByTwo:int;
private var downSwitchToZero:int;
private var lockLevel:Number;
private var lastLockTime:Number;
public function DroppedFramesRule(metrics:NetStreamMetricsBase, downSwitchByOne:int = 10, downSwitchByTwo:int = 20, downSwitchToZero:int = 24)
{
super(metrics);
this.downSwitchByOne = downSwitchByOne;
this.downSwitchByTwo = downSwitchByTwo;
this.downSwitchToZero = downSwitchToZero;
this.lastLockTime = 0;
this.lockLevel = int.MAX_VALUE;
}
override public function getNewIndex() : int
{
var moreDetail:String = null;
var newIndex:int = -1;
if(metrics.averageDroppedFPS > this.downSwitchToZero)
{
newIndex = 0;
moreDetail = "Average droppedFPS of " + Math.round(metrics.averageDroppedFPS) + " > " + this.downSwitchToZero;
}
else if(metrics.averageDroppedFPS > this.downSwitchByTwo)
{
newIndex = metrics.currentIndex - 2 < 0 ? 0 : int(metrics.currentIndex - 2);
moreDetail = "Average droppedFPS of " + Math.round(metrics.averageDroppedFPS) + " > " + this.downSwitchByTwo;
}
else if(metrics.averageDroppedFPS > this.downSwitchByOne)
{
newIndex = metrics.currentIndex - 1 < 0 ? 0 : int(metrics.currentIndex - 1);
moreDetail = "Average droppedFPS of " + Math.round(metrics.averageDroppedFPS) + " > " + this.downSwitchByOne;
}
if(newIndex != -1 && newIndex < metrics.currentIndex)
{
this.lockIndex(newIndex);
}
if(newIndex == -1 && this.isLocked(metrics.currentIndex))
{
newIndex = metrics.currentIndex;
}
return newIndex;
}
private function lockIndex(index:int) : void
{
if(!this.isLocked(index))
{
this.lockLevel = index;
this.lastLockTime = getTimer();
}
}
private function isLocked(index:int) : Boolean
{
return index >= this.lockLevel && getTimer() - this.lastLockTime < LOCK_INTERVAL;
}
}
}
@@ -0,0 +1,42 @@
package org.osmf.net.rtmpstreaming
{
import org.osmf.net.SwitchingRuleBase;
public class InsufficientBandwidthRule extends SwitchingRuleBase
{
private var bitrateMultiplier:Number;
public function InsufficientBandwidthRule(metrics:RTMPNetStreamMetrics, bitrateMultiplier:Number = 1.15)
{
super(metrics);
this.bitrateMultiplier = bitrateMultiplier;
}
override public function getNewIndex() : int
{
var moreDetail:String = null;
var i:int = 0;
var newIndex:int = -1;
if(this.rtmpMetrics.averageMaxBytesPerSecond != 0)
{
for(i = this.rtmpMetrics.currentIndex; i >= 0; i--)
{
if(this.rtmpMetrics.averageMaxBytesPerSecond * 8 / 1024 > this.rtmpMetrics.resource.streamItems[i].bitrate * this.bitrateMultiplier)
{
newIndex = i;
break;
}
}
newIndex = newIndex == this.rtmpMetrics.currentIndex ? -1 : newIndex;
}
return newIndex;
}
private function get rtmpMetrics() : RTMPNetStreamMetrics
{
return metrics as RTMPNetStreamMetrics;
}
}
}
@@ -0,0 +1,60 @@
package org.osmf.net.rtmpstreaming
{
import flash.events.NetStatusEvent;
import org.osmf.net.NetStreamCodes;
import org.osmf.net.SwitchingRuleBase;
public class InsufficientBufferRule extends SwitchingRuleBase
{
private var _panic:Boolean;
private var _moreDetail:String;
private var minBufferLength:Number;
public function InsufficientBufferRule(metrics:RTMPNetStreamMetrics, minBufferLength:Number = 2)
{
super(metrics);
this._panic = false;
this.minBufferLength = minBufferLength;
metrics.netStream.addEventListener(NetStatusEvent.NET_STATUS,this.monitorNetStatus,false,0,true);
}
override public function getNewIndex() : int
{
var newIndex:int = -1;
if(this._panic || this.rtmpMetrics.netStream.bufferLength < this.minBufferLength && this.rtmpMetrics.netStream.bufferLength > this.rtmpMetrics.netStream.bufferTime)
{
newIndex = 0;
}
return newIndex;
}
private function monitorNetStatus(e:NetStatusEvent) : void
{
switch(e.info.code)
{
case NetStreamCodes.NETSTREAM_BUFFER_FULL:
this._panic = false;
break;
case NetStreamCodes.NETSTREAM_BUFFER_EMPTY:
if(Math.round(this.rtmpMetrics.netStream.time) != 0)
{
this._panic = true;
this._moreDetail = "Buffer was empty";
}
break;
case NetStreamCodes.NETSTREAM_PLAY_INSUFFICIENTBW:
this._panic = true;
this._moreDetail = "Stream had insufficient bandwidth";
}
}
private function get rtmpMetrics() : RTMPNetStreamMetrics
{
return metrics as RTMPNetStreamMetrics;
}
}
}
@@ -0,0 +1,76 @@
package org.osmf.net.rtmpstreaming
{
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamPlayOptions;
import flash.net.NetStreamPlayTransitions;
import org.osmf.media.MediaResourceBase;
import org.osmf.net.DynamicStreamingItem;
import org.osmf.net.DynamicStreamingResource;
import org.osmf.net.NetConnectionFactoryBase;
import org.osmf.net.NetLoader;
import org.osmf.net.NetStreamLoadTrait;
import org.osmf.net.NetStreamSwitchManager;
import org.osmf.net.NetStreamSwitchManagerBase;
import org.osmf.net.NetStreamUtils;
import org.osmf.net.SwitchingRuleBase;
public class RTMPDynamicStreamingNetLoader extends NetLoader
{
public function RTMPDynamicStreamingNetLoader(factory:NetConnectionFactoryBase = null)
{
super(factory);
}
override public function canHandleResource(resource:MediaResourceBase) : Boolean
{
var dsResource:DynamicStreamingResource = resource as DynamicStreamingResource;
return dsResource != null && NetStreamUtils.isRTMPStream(dsResource.host) || Boolean(super.canHandleResource(resource));
}
override protected function createNetStreamSwitchManager(connection:NetConnection, netStream:NetStream, dsResource:DynamicStreamingResource) : NetStreamSwitchManagerBase
{
var metrics:RTMPNetStreamMetrics = null;
if(dsResource != null)
{
metrics = new RTMPNetStreamMetrics(netStream);
return new NetStreamSwitchManager(connection,netStream,dsResource,metrics,this.getDefaultSwitchingRules(metrics));
}
return null;
}
private function getDefaultSwitchingRules(metrics:RTMPNetStreamMetrics) : Vector.<SwitchingRuleBase>
{
var rules:Vector.<SwitchingRuleBase> = new Vector.<SwitchingRuleBase>();
rules.push(new SufficientBandwidthRule(metrics));
rules.push(new InsufficientBandwidthRule(metrics));
rules.push(new DroppedFramesRule(metrics));
rules.push(new InsufficientBufferRule(metrics));
return rules;
}
override protected function reconnectStream(loadTrait:NetStreamLoadTrait) : void
{
var nsPlayOptions:NetStreamPlayOptions = null;
var currentStreamItem:DynamicStreamingItem = null;
var streamName:String = null;
var dsResource:DynamicStreamingResource = loadTrait.resource as DynamicStreamingResource;
if(dsResource == null)
{
super.reconnectStream(loadTrait);
}
else
{
nsPlayOptions = new NetStreamPlayOptions();
loadTrait.netStream.attach(loadTrait.connection);
nsPlayOptions.transition = NetStreamPlayTransitions.RESUME;
currentStreamItem = dsResource.streamItems[loadTrait.switchManager.currentIndex];
streamName = currentStreamItem.streamName;
nsPlayOptions.streamName = streamName;
loadTrait.netStream.play2(nsPlayOptions);
}
}
}
}
@@ -0,0 +1,63 @@
package org.osmf.net.rtmpstreaming
{
import flash.net.NetStream;
import org.osmf.net.NetStreamMetricsBase;
import org.osmf.net.StreamType;
public class RTMPNetStreamMetrics extends NetStreamMetricsBase
{
private static const DEFAULT_AVG_MAX_BYTES_SAMPLE_SIZE:Number = 50;
private var _averageMaxBytesPerSecondArray:Array;
private var _averageMaxBytesPerSecond:Number;
public function RTMPNetStreamMetrics(netStream:NetStream)
{
super(netStream);
this._averageMaxBytesPerSecondArray = new Array();
}
public function get averageMaxBytesPerSecond() : Number
{
return this._averageMaxBytesPerSecond;
}
override protected function calculateMetrics() : void
{
var maxBytesPerSecond:Number = NaN;
var totalMaxBytesPerSecond:Number = NaN;
var peakMaxBytesPerSecond:Number = NaN;
var b:uint = 0;
super.calculateMetrics();
try
{
maxBytesPerSecond = netStream.info.maxBytesPerSecond;
this._averageMaxBytesPerSecondArray.unshift(maxBytesPerSecond);
if(this._averageMaxBytesPerSecondArray.length > DEFAULT_AVG_MAX_BYTES_SAMPLE_SIZE)
{
this._averageMaxBytesPerSecondArray.pop();
}
totalMaxBytesPerSecond = 0;
peakMaxBytesPerSecond = 0;
for(b = 0; b < this._averageMaxBytesPerSecondArray.length; b++)
{
totalMaxBytesPerSecond += this._averageMaxBytesPerSecondArray[b];
peakMaxBytesPerSecond = this._averageMaxBytesPerSecondArray[b] > peakMaxBytesPerSecond ? Number(this._averageMaxBytesPerSecondArray[b]) : peakMaxBytesPerSecond;
}
this._averageMaxBytesPerSecond = this._averageMaxBytesPerSecondArray.length < DEFAULT_AVG_MAX_BYTES_SAMPLE_SIZE ? 0 : (this.isLive ? peakMaxBytesPerSecond : totalMaxBytesPerSecond / this._averageMaxBytesPerSecondArray.length);
}
catch(error:Error)
{
throw error;
}
}
private function get isLive() : Boolean
{
return Boolean(resource) && resource.streamType == StreamType.LIVE;
}
}
}
@@ -0,0 +1,50 @@
package org.osmf.net.rtmpstreaming
{
import org.osmf.net.SwitchingRuleBase;
public class SufficientBandwidthRule extends SwitchingRuleBase
{
private static const BANDWIDTH_SAFETY_MULTIPLE:Number = 1.15;
private static const MIN_DROPPED_FPS:int = 2;
public function SufficientBandwidthRule(metrics:RTMPNetStreamMetrics)
{
super(metrics);
}
override public function getNewIndex() : int
{
var moreDetail:String = null;
var i:int = 0;
var newIndex:int = -1;
if(this.rtmpMetrics.averageMaxBytesPerSecond != 0)
{
for(i = this.rtmpMetrics.resource.streamItems.length - 1; i >= 0; i--)
{
if(this.rtmpMetrics.averageMaxBytesPerSecond * 8 / 1024 > this.rtmpMetrics.resource.streamItems[i].bitrate * BANDWIDTH_SAFETY_MULTIPLE)
{
newIndex = i;
break;
}
}
if(newIndex > this.rtmpMetrics.currentIndex)
{
newIndex = this.rtmpMetrics.droppedFPS < MIN_DROPPED_FPS && this.rtmpMetrics.netStream.bufferLength > this.rtmpMetrics.netStream.bufferTime ? newIndex : -1;
}
else
{
newIndex = -1;
}
}
return newIndex;
}
private function get rtmpMetrics() : RTMPNetStreamMetrics
{
return metrics as RTMPNetStreamMetrics;
}
}
}