关于flv:flvjs源码知识点
1 网速计算在音视频播放的场景中,用户的网速是影响体验的重要因素,播放器在播放的过程中,能够计算单位工夫获取的数据量来掂量网速。flv.js的实例提供了statistics_info事件获取以后的网速。flvPlayer.on('statistics_info', function(res) { console.log('statistics_info',res);}) res构造如下:{ currentSegmentIndex: 0,decodedFrames: 15,droppedFrames: 0,hasRedirect: false,loaderType: "fetch-stream-loader",playerType: "FlvPlayer",speed: 395.19075278358656,totalSegmentCount: 1,url: "https:/example.com/1.flv"} 其中的speed字段就是网速,单位是KB/s, 上面就看对于网速计算相干的局部。statistics_info事件中获取网速的整体流程如下图: IOController中管制每次把加载的字节数增加到SpeedSampler中,对外提供的lastSecondKBps属性是最近有数据一秒的网速。 TransmuxingController中管制播放器在加载数据的时候开启定时器获取统计数据,向上触发事件。 外围的计算还是SpeedSampler类, lastSecondKBps是getter属性获取最近有数据一秒的网速,代码含意参考正文。 get lastSecondKBps () { // 如果够1s计算 this._lastSecondBytesthis.addBytes(0)// 上1秒的_lastSecondBytes有数据 就间接返回// 这个奇妙的是 感觉不是精确的1s 然而又是精确的 因为如果是超过1秒就不持续增加了 1秒内的就增加进去了。// 如果上一秒有数据则返回if (this._lastSecondBytes !== 0) { return this._lastSecondBytes / 1024} else { // 如果上一秒的速度是0,并且间隔上次计算超过了500ms 则用_intervalBytes和durationSeconds进行计算 if (this._now() - this._lastCheckpoint >= 500) { // if time interval since last checkpoint has exceeded 500ms // the speed is nearly accurate return this.currentKBps } else { // We don't know return 0 }}} ...