|
@@ -802,6 +802,8 @@
|
|
|
totalTraffic: 0, // 总流量(字节)
|
|
totalTraffic: 0, // 总流量(字节)
|
|
|
bitrate: 800, // 录播默认码率 0.16Mbps
|
|
bitrate: 800, // 录播默认码率 0.16Mbps
|
|
|
bitrateLive: 1600, // 直播默认码率 0.16Mbps
|
|
bitrateLive: 1600, // 直播默认码率 0.16Mbps
|
|
|
|
|
+ videoLoaded: false, // 视频是否加载成功
|
|
|
|
|
+ trafficStartTime: 0, // 流量计算开始时间
|
|
|
|
|
|
|
|
//定时器
|
|
//定时器
|
|
|
trafficTimer: null,
|
|
trafficTimer: null,
|
|
@@ -1191,6 +1193,9 @@
|
|
|
this.startTime = 0;
|
|
this.startTime = 0;
|
|
|
this.totalTraffic = 0;
|
|
this.totalTraffic = 0;
|
|
|
}
|
|
}
|
|
|
|
|
+ // 重置视频加载状态和流量计算开始时间
|
|
|
|
|
+ this.videoLoaded = false;
|
|
|
|
|
+ this.trafficStartTime = 0;
|
|
|
this.startTimer();
|
|
this.startTimer();
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
|
this.$nextTick(() => {
|
|
@@ -1350,8 +1355,14 @@
|
|
|
// 清除所有定时器(使用增强清理)
|
|
// 清除所有定时器(使用增强清理)
|
|
|
// this.clearAllTimersEnhanced();
|
|
// this.clearAllTimersEnhanced();
|
|
|
// this.stopHeartBeat();
|
|
// this.stopHeartBeat();
|
|
|
|
|
+ // 页面隐藏时,提交当前流量数据
|
|
|
|
|
+ if (this.videoLoaded && this.trafficStartTime && this.trafficTimer) {
|
|
|
|
|
+ const watchDuration = Math.floor((Date.now() - this.trafficStartTime) / 1000);
|
|
|
|
|
+ this.submitTraffic(watchDuration);
|
|
|
|
|
+ }
|
|
|
if (this.trafficTimer) {
|
|
if (this.trafficTimer) {
|
|
|
clearInterval(this.trafficTimer);
|
|
clearInterval(this.trafficTimer);
|
|
|
|
|
+ this.trafficTimer = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 页面隐藏时清理部分数据,减少内存占用
|
|
// 页面隐藏时清理部分数据,减少内存占用
|
|
@@ -1368,6 +1379,14 @@
|
|
|
// 保存视频进度
|
|
// 保存视频进度
|
|
|
this.saveVideoProgress();
|
|
this.saveVideoProgress();
|
|
|
|
|
|
|
|
|
|
+ // 用户退出时,再次请求一次10s流量接口
|
|
|
|
|
+ if (this.videoLoaded && this.trafficStartTime) {
|
|
|
|
|
+ const watchDuration = Math.floor((Date.now() - this.trafficStartTime) / 1000);
|
|
|
|
|
+ // 提交最后一次流量数据(至少10秒)
|
|
|
|
|
+ const finalDuration = Math.max(watchDuration, 10);
|
|
|
|
|
+ this.submitTraffic(finalDuration);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 清理直播相关定时器
|
|
// 清理直播相关定时器
|
|
|
if (this.liveItem) {
|
|
if (this.liveItem) {
|
|
|
this.pauseVideo();
|
|
this.pauseVideo();
|
|
@@ -2602,62 +2621,110 @@
|
|
|
// that.calculateTraffic(bitrate);
|
|
// that.calculateTraffic(bitrate);
|
|
|
// }, 10000); // 每10秒计算一次
|
|
// }, 10000); // 每10秒计算一次
|
|
|
// },
|
|
// },
|
|
|
- //直播计算流量
|
|
|
|
|
|
|
+ //直播计算流量 - 统一流量计算方法
|
|
|
startTrafficCalculation() {
|
|
startTrafficCalculation() {
|
|
|
|
|
+ // 检查必要数据
|
|
|
|
|
+ if (!this.liveItem || !this.liveItem.videoFileSize) {
|
|
|
|
|
+ console.warn('视频文件大小数据不完整,无法启动流量计算');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查视频总时长
|
|
|
|
|
+ const totalDuration = this.liveItem.videoDuration || this.liveItem.duration;
|
|
|
|
|
+ if (!totalDuration) {
|
|
|
|
|
+ console.warn('视频总时长数据不完整,无法启动流量计算');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 清除已有定时器
|
|
|
if (this.trafficTimer) {
|
|
if (this.trafficTimer) {
|
|
|
clearInterval(this.trafficTimer);
|
|
clearInterval(this.trafficTimer);
|
|
|
this.trafficTimer = null;
|
|
this.trafficTimer = null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- this.startTime = Date.now();
|
|
|
|
|
|
|
+ // 记录流量计算开始时间
|
|
|
|
|
+ this.trafficStartTime = Date.now();
|
|
|
var that = this;
|
|
var that = this;
|
|
|
|
|
|
|
|
- // 计算码率
|
|
|
|
|
- let bitrate = this.calculateBitrate();
|
|
|
|
|
|
|
+ // 立即提交一次10s流量(模拟10秒观看)
|
|
|
|
|
+ setTimeout(() => {
|
|
|
|
|
+ that.submitTraffic(10);
|
|
|
|
|
+ }, 100); // 延迟100ms确保数据已准备好
|
|
|
|
|
|
|
|
|
|
+ // 启动定时器,每10秒计算并提交一次流量
|
|
|
this.trafficTimer = setInterval(() => {
|
|
this.trafficTimer = setInterval(() => {
|
|
|
- that.calculateTraffic(bitrate);
|
|
|
|
|
|
|
+ that.calculateAndSubmitTraffic();
|
|
|
}, 10000); // 每10秒计算一次
|
|
}, 10000); // 每10秒计算一次
|
|
|
},
|
|
},
|
|
|
- // 计算流量
|
|
|
|
|
- // calculateTraffic(bitrate) {
|
|
|
|
|
- // const currentTime = Date.now();
|
|
|
|
|
- // const duration = (currentTime - this.startTime) / 1000; // 持续时间(秒)
|
|
|
|
|
- // // 流量 = 码率 × 时间
|
|
|
|
|
- // // 码率单位: bps, 时间单位: 秒, 流量单位: 比特
|
|
|
|
|
- // const trafficBits = bitrate * duration;
|
|
|
|
|
- // // 转换为字节
|
|
|
|
|
- // this.totalTraffic = trafficBits / 8;
|
|
|
|
|
- // this.getLiveInternetTraffic();
|
|
|
|
|
- // },
|
|
|
|
|
- calculateBitrate() {
|
|
|
|
|
- // 如果接口返回了视频文件大小和时长,使用这些数据计算码率
|
|
|
|
|
- if (this.liveItem.videoFileSize && this.liveItem.videoDuration) {
|
|
|
|
|
- // 码率 = 文件大小(字节) / 时长(秒) × 8 (转换为bps) × 5
|
|
|
|
|
- const calculatedBitrate = (this.liveItem.videoFileSize / this.liveItem.videoDuration) * 8 * 5;
|
|
|
|
|
- console.log(
|
|
|
|
|
- `使用接口数据计算码率: ${calculatedBitrate} bps (文件大小: ${this.liveItem.videoFileSize} 字节, 时长: ${this.liveItem.videoDuration} 秒)`
|
|
|
|
|
- );
|
|
|
|
|
- return calculatedBitrate;
|
|
|
|
|
- } else {
|
|
|
|
|
- // 如果任一字段为空,使用默认码率 1500 bps
|
|
|
|
|
- console.log('接口数据不完整,使用默认码率: 1500 bps');
|
|
|
|
|
- return 1500;
|
|
|
|
|
|
|
+ // 计算并提交流量
|
|
|
|
|
+ calculateAndSubmitTraffic() {
|
|
|
|
|
+ if (!this.liveItem || !this.liveItem.videoFileSize) {
|
|
|
|
|
+ const totalDuration = this.liveItem.videoDuration || this.liveItem.duration;
|
|
|
|
|
+ if (!totalDuration) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 计算用户观看视频时长(秒)
|
|
|
|
|
+ const watchDuration = Math.floor((Date.now() - this.trafficStartTime) / 1000);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取视频总时长(秒)
|
|
|
|
|
+ const totalDuration = this.liveItem.videoDuration || this.liveItem.duration || 0;
|
|
|
|
|
+
|
|
|
|
|
+ // 获取视频文件大小(字节)
|
|
|
|
|
+ const videoFileSize = this.liveItem.videoFileSize || 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (totalDuration <= 0 || videoFileSize <= 0) {
|
|
|
|
|
+ console.warn('视频总时长或文件大小无效,无法计算流量');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 流量计算方法:用户观看视频时长/视频总时长*视频文件大小
|
|
|
|
|
+ const calculatedTraffic = (watchDuration / totalDuration) * videoFileSize;
|
|
|
|
|
+
|
|
|
|
|
+ // 更新总流量
|
|
|
|
|
+ this.totalTraffic = calculatedTraffic;
|
|
|
|
|
+
|
|
|
|
|
+ // 提交流量数据
|
|
|
|
|
+ this.submitTraffic(watchDuration);
|
|
|
},
|
|
},
|
|
|
- calculateTraffic(bitrate) {
|
|
|
|
|
- const currentTime = Date.now();
|
|
|
|
|
- const duration = (currentTime - this.startTime) / 1000; // 持续时间(秒)
|
|
|
|
|
|
|
+ // 提交流量数据到后端
|
|
|
|
|
+ submitTraffic(watchDuration) {
|
|
|
|
|
+ if (!this.liveId || !this.userInfo || !this.userInfo.userId) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 流量 = 码率 × 时间
|
|
|
|
|
- // 码率单位: bps, 时间单位: 秒, 流量单位: 比特
|
|
|
|
|
- const trafficBits = bitrate * duration;
|
|
|
|
|
|
|
+ // 计算当前观看时长对应的流量
|
|
|
|
|
+ const totalDuration = this.liveItem.videoDuration || this.liveItem.duration || 0;
|
|
|
|
|
+ const videoFileSize = this.liveItem.videoFileSize || 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (totalDuration <= 0 || videoFileSize <= 0) {
|
|
|
|
|
+ console.warn('视频总时长或文件大小无效,无法提交流量', {
|
|
|
|
|
+ totalDuration,
|
|
|
|
|
+ videoFileSize,
|
|
|
|
|
+ videoDuration: this.liveItem.videoDuration,
|
|
|
|
|
+ duration: this.liveItem.duration
|
|
|
|
|
+ });
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 转换为字节
|
|
|
|
|
- this.totalTraffic = trafficBits / 8;
|
|
|
|
|
|
|
+ // 流量 = 用户观看视频时长/视频总时长*视频文件大小
|
|
|
|
|
+ const traffic = (watchDuration / totalDuration) * videoFileSize;
|
|
|
|
|
|
|
|
- // 调用流量上报接口
|
|
|
|
|
- this.getLiveInternetTraffic();
|
|
|
|
|
|
|
+ const param = {
|
|
|
|
|
+ userId: this.userInfo.userId || '',
|
|
|
|
|
+ liveId: this.liveId || '',
|
|
|
|
|
+ uuId: dayjs().format('YYYYMMDD') + this.uuId,
|
|
|
|
|
+ internetTraffic: Math.round(traffic), // 四舍五入到整数
|
|
|
|
|
+ watchDuration: watchDuration, // 观看时长(秒)
|
|
|
|
|
+ totalDuration: totalDuration, // 视频总时长(秒)
|
|
|
|
|
+ videoFileSize: videoFileSize // 视频文件大小(字节)
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ console.log('提交流量数据:', param);
|
|
|
|
|
+ liveInternetTraffic(param).catch(err => {
|
|
|
|
|
+ console.error('流量数据提交失败:', err);
|
|
|
|
|
+ });
|
|
|
},
|
|
},
|
|
|
startTimer() {
|
|
startTimer() {
|
|
|
this.startTime = Date.now();
|
|
this.startTime = Date.now();
|
|
@@ -3258,6 +3325,15 @@
|
|
|
// }
|
|
// }
|
|
|
this.videoProgressKey = `videoProgress_${this.liveId}`;
|
|
this.videoProgressKey = `videoProgress_${this.liveId}`;
|
|
|
this.setVideoProgress();
|
|
this.setVideoProgress();
|
|
|
|
|
+
|
|
|
|
|
+ // 视频加载成功后,启动流量计算
|
|
|
|
|
+ if (!this.videoLoaded) {
|
|
|
|
|
+ this.videoLoaded = true;
|
|
|
|
|
+ // 延迟一下确保数据已加载
|
|
|
|
|
+ setTimeout(() => {
|
|
|
|
|
+ this.startTrafficCalculation();
|
|
|
|
|
+ }, 500);
|
|
|
|
|
+ }
|
|
|
},
|
|
},
|
|
|
setVideoProgress() {
|
|
setVideoProgress() {
|
|
|
// 只有录播和回放需要设置进度
|
|
// 只有录播和回放需要设置进度
|