Playing video seamlessly even when your train enters a tunnel.
When you upload a video, the server Transcodes it into a "Bitrate Ladder" — multiple copies of the video at different resolutions (1080p, 720p, 360p) and cuts them into tiny 4-second chunks.
When you watch the video, the player constantly monitors your internet speed. If your connection drops, it performs Adaptive Bitrate (ABR) Streaming: it seamlessly switches to requesting the 360p chunks instead of the 1080p chunks, preventing the video from buffering and pausing.
def get_next_chunk(chunk_index, current_bandwidth):
# Player constantly measures download speed of the last chunk
if current_bandwidth > 5.0_Mbps:
quality = "1080p"
elif current_bandwidth > 2.0_Mbps:
quality = "720p"
else:
quality = "360p" # Save the stream from buffering!
return fetch_from_cdn(quality, chunk_index)