shaka-player icon indicating copy to clipboard operation
shaka-player copied to clipboard

I want to maintain a constant time difference at all times.

Open brodiddev opened this issue 10 months ago • 0 comments

Have you read the Tutorials? yes

Have you read the FAQ and checked for duplicate open issues? yes

If the question is related to FairPlay, have you read the tutorial?

yes

What version of Shaka Player are you using? 4.7.11

What browser and OS are you using? window chrome

Please ask your question

I want to maintain a constant time difference at all times.

With the code below, the left buffer is wide, from 0 to 4.5 seconds, and the buffer is maintained unstable.

I always wanted to keep the leftBuffer in the 2-second range, so I thought about the method below. Is there a better alternative?

my configure

abr: { enabled: false },
streaming: {
	bufferingGoal: 10,
	rebufferingGoal: 3,
	lowLatencyMode: true,
	startAtSegmentBoundary: true,
	liveSyncMaxLatency: 0.5,
	updateIntervalSeconds: 0.5,
	retryParameters: { baseDelay: 1000, timeout: 5000, maxAttempts: 2 },
},
manifest: {
	hls: {
		liveSegmentsDelay: 0.8,
	},
	retryParameters: { baseDelay: 1000, timeout: 5000, maxAttempts: 2 },
},

buffering Event

onBufferingEvent(event) {
const bufferState = event.buffering;
const bufferedInfo = this.calculateBufferedInfo(this.videoElement);

if (bufferState && bufferedInfo.leftFormatted < 2) {
	this.player.configure({ streaming: { bufferingGoal: 12 } });
} else if (!bufferState && bufferedInfo.leftFormatted > 2) {
	this.player.configure({ streaming: { bufferingGoal: 10 } });
}
}

leftbuffer calculation

calculateBufferedInfo(videoElement) {
	const position = videoElement.currentTime;
	let bufferedInfo = { total: 0, played: 0, left: 0 };

	for (let i = 0; i < videoElement.buffered.length; i++) {
		const start = videoElement.buffered.start(i);
		const end = videoElement.buffered.end(i);
		const duration = end - start;

		if (start <= position && end > position) {
			bufferedInfo.played += Math.max(0, position - start);
			bufferedInfo.left += Math.max(0, end - position);
		} else if (start > position) {
			bufferedInfo.left += duration;
		} else {
			bufferedInfo.played += duration;
		}

		bufferedInfo.total += duration;
	}
	const leftFormatted = bufferedInfo.left.toFixed(3);
	const playedFormatted = bufferedInfo.played.toFixed(3);
	const totalFormatted = bufferedInfo.total.toFixed(3);

	return { leftFormatted, playedFormatted, totalFormatted };
}

brodiddev avatar May 02 '24 07:05 brodiddev