title: How does Youtube's HTML5 video player control buffering?
body: <p>I was watching a youtube video and I decided to investigate some parts of its video player.  I noticed that unlike most HTML5 video I have seen, Youtube's video player does not do a normal video source and instead utilizes a blob url as the source.  </p>
  
  <p>Previously I have tested HTML5 videos and I found that the server starts streaming the whole video from  the start and buffers in the background the complete rest of the video.  This means that if your video is 300 megs, all 300 megs will be downloaded.  If you seek to the middle, it will start downloading from the seek position all the way to the end.  </p>
  
  <p>Youtube does not work this way (at least in chrome).  Instead it manages to control buffering so it only buffers a certain amount while paused. It also seems to only buffer the relevant pieces, so if you skip around it will make sure not to buffer pieces that are unlikely to be watched.</p>
  
  <p>In my attempts to investigate how this worked, I noticed the video src tag has a value of <code>blob:http%3A//www.youtube.com/ee625eee-2802-49b2-a13f-eb374d551d54</code>, which pointed me to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob" rel="noreferrer">blobs</a>, which then led me to <a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data" rel="noreferrer">typed arrays</a>.  Using those two resources I am able to load a mp4 video into a blob and display it in a HTML5 video tag.</p>
  
  <p>However, what I am now stuck on is how Youtube deals with the pieces.  Looking at the network traffic it appears to sends requests to <code>http://r6---sn-p5q7ynee.c.youtube.com/videoplayback</code> which returns binary video data back in chunks of 1.1mb.  It also seems worth noting that most normal requests due to HTML5 video requests seem to receive a 206 response code back while it streams, yet youtube's playvideo calls get a 200 back.</p>
  
  <p>I tried to attempt to only load a range of bytes (via setting the <code>Range</code> http header) which unfortunately failed (I'm assuming because there was no meta-data for the video coming with the video).  </p>
  
  <p>At this point I'm stuck on figuring out how Youtube accomplishes this.  I came up with several ideas though none of which I am completely sold on:</p>
  
  <p>1) Youtube is sending down self contained video and audio chunks with each <code>/videoplayback</code> call.  This seems like a pretty heavy burden on the upload side and it seems like it would be difficult to stitch these together to make it appear like it's one seemless video.  Also, the video tag seems to think it's one full video, judging from calling <code>$('video').duration</code> and <code>$('video').currentTime</code>, which leads me to believe that the video tag thinks it's a single video file.  Finally, the vidoe src tag never changes which makes me believe it is working with a singular blob and not switching out blobs.</p>
  
  <p>2) Youtube constructs an empty blob pre-sized to the full video array and updates the blob with pieces as it downloads it.  It would then make sure the user has not gotten too close to the last downloaded piece (to prevent the user from entering an undownloaded section of the blob).  The problem that I see with this that I don't see any way to dynamically update a blob through javascript (although maybe I'm just having trouble googling for it)</p>
  
  <p>3) Youtube downloads the meta data and then starts constructing the blob in order by appending the video pieces as it downloads them.  The problem I see with this method is I don't understand how it would handle seeks in post-buffered territory.</p>
  
  <p>Maybe I"m just missing an obvious answer that's right in front of me.  Anyone have any ideas?</p>
  
  <p><hr>
  edit: I just thought of a fourth option.  Another idea is they might use the file API to write the binary chunks to a file and use that file to stream off of.  The file API seems to have the ability to seek to specific positions, therefore allowing you to fill a video with empty bytes and fill them in as they are received.  This would definitely accommodate video seeking as well.</p>