Search code examples
flashflvplayback

Flash content download


I've been wondering something, websites like youtube (pandora does mp4s)

  • play their music through a flash player
  • while each track is played, they're downloaded to the user's computer, e.g., /tmp/FlashXX*****

This allows users to go and move the flv out of that folder for later playback.

However, when the user moves the flv out of the /tmp folder, the player continues to play the music/video quite happily. How does the flash player handle the removal of its file and why doesn't it throw errors from this?

More importantly, why are the flvs downloaded to the user in the first place if the player plays happily without them?


Solution

  • This actually has nothing to do with caching. Rather, it works due to the way Unix filesystems work. When a file is opened, its inode is read and indicates where the data is located on the disk. When a file is moved or deleted ("unlinked"), the inode is moved to a new location or removed from the directory. However, the data it points to is not invalidated until no reference to that inode exists. In other words, as long as a file remains open, deleting or moving it has no effect on a program reading that file.

    This is not how Windows filesystems work, and that is was leads to the common "file is locked" problems when trying to delete files that are in use. (Disclaimer: This may not be true with NTFS, which supports hard links, but I'm pretty sure it was the case on FAT.)

    It works so well that sometimes programs will even use a trick to automatically clean up after itself by creating a file and "removing" it right away, while keeping it open. That way, the temporary file can continue to be used until the program is done with it, at which time it automatically "disappears".

    By the way, programs that expect to be able to close and reopen a temporary file will get confused if you move them. You can avoid this by making a "copy" using a hard link.

    ln /tmp/Fl* .
    

    This command will create copies of the inode referenced by Flash for the temporary file into the current directory. It doesn't actually copy the content of the file, only creates a second reference to the same data on disk. That way when Flash closes, you still have a "pointer" to the data that it released.

    I haven't studied this topic in some time and I might have gotten some terminology wrong, so I suggest if you want to understand further, read up on inodes and how hard links work.

    From that Wikipedia link:

    The process of unlinking dissociates a name from the data on the volume without destroying the associated data. The data are still accessible as long as at least one link that points to it still exists. When the last link is removed, the space is considered free.