Using a RAM drive for the live TV buffer

Introduction
First and foremost, this wiki is not meant for a beginner or even an intermediate user. This wiki is meant for advanced users. There is quite a bit of technical information below, and it may be difficult to comprehend. If you do not fully comprehend what you are reading, do not attempt anything!

This wiki will explain how to set up and configure a RAM drive for the live TV buffer. For the purpose of simplicity, this wiki will assume the reader is using a system without extenders.

So why would anyone want to set up a RAM drive for the live TV buffer? Unlike a solid state drive or a USB flash drive, RAM can be written to and erased from an unlimited number of times. And unlike a HDD, RAM is not mechanical, which means it is not subject to mechanical wear. RAM is also fast. It's not just fast....it's extremely fast. The read and write speed of RAM surpasses any storage method on the market today. If a person wants to have the live TV buffer on the fastest possible device, it should be stored in RAM.

Prerequisites
To perform this task, you should be familiar with performing two other tasks: This wiki will assume you have read the above two wikis and fully understand how to perform both tasks.
 * Changing the Location of the Live TV Buffer
 * Changing the Length of the Live TV Buffer

You will also need software for creating a drive out of system RAM, and it helps to have a calculator handy (unless you just prefer the old pencil-and-paper method...or if you are just that good at math).

Explanation of the Live TV Buffering Process
Before you can create a RAM drive and move the live TV buffer to that drive, you must answer one question: How much RAM is required? In order to answer that question, you need to understand the live TV buffering process, what files live TV buffering requires, and how big those files are going to be. This section will explain the live TV buffering process and what files are required.

Terminology
In this wiki, the following terms will be used to describe each type of buffer file that exists:
 * Initial: When WMC starts, it creates two initial buffer files per tuner. Each file is assigned to a specific tuner, and when that tuner is used for live TV, the two initial buffer files will be used to start the buffering process.
 * Inactive: A group of initial buffer files that are assigned to inactive tuners.
 * Current: The file WMC is currently writing.
 * Next: The next file WMC will use for buffering.
 * Old/closed: A file that has already been written.
 * Oldest: The oldest closed buffer file.

How Windows Media Center Allocates Space for the Live TV Buffer
The live TV buffer files are controlled by both hard-coding in Windows Media Center and values in the registry. When WMC is started, it will create two initial buffer files per tuner in the live TV buffer folder. The size of those files depends on the amount of free space available on the drive. If there is enough free space, WMC will try to create buffer files with a size of 510,720 KB. If there is not enough free space available, WMC will revert to a file size of 72,960 KB. After live TV has started, WMC will use the same method for creating each of the next buffer files. Here is an example of what you would see if you were looking at the contents of the buffer folder on a dual tuner system before live TV playback was started:
 * File 1 this is an initial file assigned to tuner 1
 * File 2 this is an initial file assigned to tuner 1
 * File 3 this is an initial file assigned to tuner 2
 * File 4 this is an initial file assigned to tuner 2

How Windows Media Center Performs Live TV Buffering After Space is Allocated
After WMC has been started, there will be a number of initial buffer files in the buffer folder. Each of these files will be assigned to a specific tuner; initially there are two files created per tuner. When a tuner is activated as the live TV tuner, WMC will start writing to the first live TV buffer file assigned to that tuner, making that buffer file the current buffer file. Just before the amount of time specified in BackingStoreEachFileDurationSeconds has passed, WMC will then open the second file for writing. The second file then becomes the current buffer file, and the first file becomes an old buffer file. Just before the amount of time specified in BackingStoreEachFileDurationSeconds has passed, WMC will then create the next buffer file. The next buffer file then becomes the current buffer file, and what was just the current buffer file becomes an old buffer file. This process will repeat until the number of old/closed buffer files equals the value of BackingStoreMaxNumBackingFiles. Once the number of old/closed files equals the value of BackingStoreMaxNumBackingFiles, the process changes slightly. The next time WMC closes a buffer file, it will then delete the oldest buffer file, ensuring the total number of old buffer files does not exceed the value of BackingStoreMaxNumBackingFiles. This process will then continue indefinitely until the tuner is released from use.

Here is the action you would see if you were monitoring the buffering of live TV on a single tuner with a BackingStoreMaxNumBackingFiles value of 4: In step 11, the oldest file was deleted in order to maintain 4 old/closed files. Steps 9 through 11 would then be repeated indefinitely until live TV playback is stopped. Once the tuner is released from use, all buffer files are then deleted, and the two initial buffer files are then re-created. Note: Even though a tuner may not be actively buffering, it may not have been released yet. This occurs specifically when a tuner is used for live TV, then the user presses the record button. The recording is using the same tuner, so live TV buffering is suspended for that tuner. However, the tuner is not free, so the buffer files are not cleared. If the user changes channels, a second tuner will then be used for live TV, and the "initial" buffer files assigned for that tuner will be used. You could potentially activate a live TV buffer for every tuner in your system, which would significantly impact your ability to use a RAM drive. Therefore, use of the record button during live TV is highly discouraged when using a RAM drive. Instead, stop live TV playback, then start the recording. This will ensure only one group of buffer files exists at a time.
 * Step 1: Buffering started
 * File 1: This is the current file
 * File 2: This is an initial file assigned to tuner 1 and is also the next file
 * Step 2: Buffering transferred to next file
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is the current file
 * Step 3: Next file created
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is the current file
 * File 3: This is the next file
 * Step 4: Buffering transferred to next file
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is the current file
 * Step 5: Next file created
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is the current file
 * File 4: This is the next file
 * Step 6: Buffering transferred to next file
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is an old/closed file
 * File 4: This is the current file
 * Step 7: Next file created
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is an old/closed file
 * File 4: This is the current file
 * File 5: This is the next file
 * Step 8: Buffering transferred to next file
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is an old/closed file
 * File 4: This is an old/closed file
 * File 5: This is the current file
 * Step 9: Next file created
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is an old/closed file
 * File 4: This is an old/closed file
 * File 5: This is the current file
 * File 6: This is the next file
 * Step 10: Buffering transferred to next file
 * File 1: This is an old/closed file and also the oldest file
 * File 2: This is an old/closed file
 * File 3: This is an old/closed file
 * File 4: This is an old/closed file
 * File 5: This is an old/closed file
 * File 6: This is the current file
 * Step 11: Oldest file deleted
 * File 2: This is an old/closed file and also the oldest file
 * File 3: This is an old/closed file
 * File 4: This is an old/closed file
 * File 5: This is an old/closed file
 * File 6: This is the current file

Now that you know the live TV buffering process and what files are created, you can start calculating how much space is required for those files.

Computing Space Requirements - Basic Method
The first step to creating a RAM drive is to determine how much RAM is required. This is a 3-step process:
 * 1) Determine how much RAM is required for the live TV buffer.
 * 2) Determine how much RAM is required for the inactive buffer files.
 * 3) Add the two numbers together to find the total amount of RAM required.

Determine how much RAM is required for the live TV buffer
The total live TV buffer size in WMC is calculated using the following formula:

Formula 1: ((BackingStoreMaxNumBackingFiles + 1) * BackingStoreEachFileDurationSeconds * Maximum Bitrate) + Next File Filesize

One is added to the value of BackingStoreEachFileDurationSeconds because BackingStoreEachFileDurationSeconds only defines the maximum number of old buffer files that can exist. However, you must also account for the current file being written (and thus is not old yet). Additionally, you must have enough free space to create the next buffer file WMC will use. As was previously explained, the file size will depend on how much free space is available, and will be either 510,720 KB or 72,960 KB. For simplicity, I will ignore the option for the files to have the reduced size for now, but I will cover it in a later section in this wiki.

In the US, digital TV broadcasts are limited to a bitrate of approximately 20 Mbps, which is 2.5 MB/s, or 2,560 KB/s. To calculate the amount of free space you need for the live TV buffer, simply plug in the numbers to the formula above.

As an example, the default value for BackingStoreEachFileDurationSeconds is 300 and the default value for BackingStoreMaxNumBackingFiles is 8. Plugging this in to the formula listed above, we get:

Example A: ((8 + 1) * 300 * 2,560 KB) + 510,720 KB = 7,422,720 KB (7,248.75 MB)

So using the default numbers, the space required for the live TV buffer is 7,248.75 MB.

Tip: To conserve space, use a large number of buffer files with a small duration rather than a small number of buffer files with a large duration. The minimum allowed duration is 5 seconds. By using a 5 second duration, you will realize the smallest possible space requirement.

Determine how much RAM is required for the inactive buffer files
In addition to having enough free space for the live TV buffer, you also need enough space on the RAM drive for the buffer files WMC creates for the inactive tuners (the inactive buffer files). This is fairly simple to calculate. Simply multiply the number of inactive tuners by 2, then multiply that by 510,720 KB.

Formula 2: Inactive tuners * 2 * 510,720 KB

Add the two numbers together to find the total amount of RAM required
Simply add the amount of space required for the live TV buffer to the amount of space required for the inactive buffer files.

Here is an example:

Example B: Q: I have a 4-tuner system and want a 9 minute live TV buffer. How much space do I need?

A: Simply plug the numbers into the formulas: ((108 + 1) * 5 * 2,560 KB) + 510,720 KB = 1,905,920 KB (1,861.25 MB) 6 * 510,720 KB = 3,064,320 KB (2,992.5 MB) But what if there was a way to get the same 9-minute buffer using less space? Scroll to the very last section of this wiki, titled "Computing Space Requirements - Advanced Method" to learn more.
 * The 9-minute live TV buffer can be achieved by using 108 buffer files with a duration of 5 seconds each. Plugging the numbers into Formula 1, we get this:
 * A 4-tuner system will require 8 initial buffer files. 6 of these files will remain inactive while 2 will be used for the live TV buffer. Plugging these number into Formula 2, we get:
 * Summing the above two answers, we get a total space requirement of 4853.75 MB.

Creating the RAM Drive and Setting it as the Location for the Live TV Buffer
Once you know how much RAM you need, the next step is to actually create the RAM drive and set it as the location for the live TV buffer.

There are many different programs you can use to create a RAM drive. Start by finding a program with a command-line interface so the commands can be scripted and executed as part of the Windows 7 boot process. Once you have the software installed, create a script that will create the RAM drive and then format it. If the drive isn't formatted, it won't be usable. Some RAM drive programs even have optional switches that will automatically format the drive after it is created. Once the script is created, test it to make sure it works, then set the system up to automatically execute the script when the system is booted. This can be accomplished several different ways, such as via the "Startup" folder, via the registry, or via Task Scheduler. Reboot to test the functionality of the script. Note: Not all software is created the same. Some RAM drive programs take a long time to create a RAM drive, while other programs can create a RAM drive in the blink of an eye. Choose your RAM drive program carefully.

Once the computer is successfully booting and creating the RAM drive, change the live TV buffer location to the RAM drive. CAUTION: IF YOU CHANGE THE PATH IN THE REGISTRY AND THE PATH EVER BECOMES INVALID, WMC WILL RESTORE THE DEFAULT PATH TO THE LIVE TV BUFFER (C:\USERS\PUBLIC\RECORDED TV\). THEREFORE, CHANGING THE PATH IN THE REGISTRY IS NOT ADVISED. INSTEAD, CREATE A SYMLINK OR A JUNCTION TO THE DESIRED PATH. THIS WILL ENSURE THE PATH IN THE REGISTRY IS ALWAYS VALID.

Next, start WMC and enjoy having live TV on a RAM drive!

Computing Space Requirements - Advanced Method
For most people, the basic method will be sufficient. However, it requires a lot of space, especially if you have multiple tuners. This section will focus specifically on decreasing the amount of space required by taking advantage of the 72,960 KB file size referenced earlier in the wiki. This could potentially save you hundreds or even thousands of MB's of RAM.

One of the biggest problems with the basic method explained above is the amount of space required for the inactive buffer files. Nearly 1 GB per tuner is quite a large amount of RAM if you have more than just one or two tuners. Luckily, there is a way to overcome this. By limiting the amount of free space on the drive during the buffer file creation process, the inactive buffer files can still be created, but they won't require as much space.

510,720 and 72,960 have a relationship....510,720 is 7 * 72,960. By limiting the free space to below 510,720 KB, you can force up to 6 of the inactive tuner files to be sized at 72,960. However, this comes with a tradeoff: You don't have much free space for the live TV buffer. So the question is, how much buffer can you fit in the free space? The amount of space you can use for the live TV buffer depends on where in the file creation process the drive has less than 510,720 KB free. There are 7 possible times when the free space on the drive can drop below 510,720 KB: Remember, 72,960 * 7 = 510,720. So if the drive had less than 510,720 KB free after all but 7 of the initial buffer files were created, there would not be enough free space on the drive to create all the initial buffer files, and live TV could potential fail.
 * 1) Immediately after all initial buffer files have been created
 * 2) Immediately after all but 1 of the initial buffer files have been created
 * 3) Immediately after all but 2 of the initial buffer files have been created
 * 4) Immediately after all but 3 of the initial buffer files have been created
 * 5) Immediately after all but 4 of the initial buffer files have been created
 * 6) Immediately after all but 5 of the initial buffer files have been created
 * 7) Immediately after all but 6 of the initial buffer files have been created

Regardless of when the available free space drops below 510,720 KB, the remaining free space must be enough to create all remaining initial buffer files plus store the live TV buffer. But the question becomes, how much buffer can you fit in a given amount of free space. Well, that depends on which of the above 7 scenarios is actually happening. Scenario 1 will offer the biggest buffer with the smallest space saving, while scenario 7 will offer the biggest space saving, but will have the smallest buffer. You will need to determine which of the scenarios works for you.

Scenario 1: The free space drops below 510,720 KB immediately after all initial buffer files have been created
Start by assuming the free space is at the upper limit...510,719 KB. Two of the initial buffer files will be used for live TV buffering. Therefore, the space they are taking up on the drive can be added back in to the total free space to calculate how much space is available for the live TV buffer. In this case, that means adding back 2 * 510,720 KB, or 1,021,440 KB. Plugging this information into Formula 1, we get:

((BackingStoreMaxNumBackingFiles + 1) * BackingStoreEachFileDurationSeconds * 2,560 KB) + Next File Filesize <= 1,532,159 KB

The goal in this section is to conserve space, so BackingStoreEachFileDurationSeconds should be as small as possible (5). That yields the following formula:

((BackingStoreMaxNumBackingFiles + 1) * 12,800 KB) + Next File Filesize <= 1,532,159 KB

At this point, simply plug in 72,960 KB for Next File Filesize. This give you the following formula:

((BackingStoreMaxNumBackingFiles + 1) * 12,800 KB) + 72,960 KB <= 1,532,159 KB

Which further yields:

BackingStoreMaxNumBackingFiles <= 112.99

Round the number down to the nearest whole number to determine the maximum value for BackingStoreMaxNumBackingFiles. If the answer is already a whole number, just subtract 1. In this example, there is only enough free space for BackingStoreMaxNumBackingFiles to be any number less than or equal to 112. Going back to Example B from the previous section, the desired value for BackingStoreMaxNumBackingFiles was 108. This drive has enough free space for 4 additional buffer files, but they will not be used. So subtract the required space for 4 buffer files from amount of free space left on the drive:

510,719 KB - (4 * (5 * 2560 KB)) = 459,519 KB (448.75 MB)

This is the amount of space required for the live TV buffer. Adding in the amount of space required for the initial buffer files from Example 2 (2992.5 MB), the total space required using this method is 3441.25 MB. That's a savings of 1412.5 MB over the basic calculation method, and in both cases the buffer is the same size!

Sounds easy, right? Not so fast! It gets harder...

Scenario 2: The free space drops below 510,720 KB after all but 1 of the initial buffer files are created
This is where things get tricky. In this scenario, the last initial buffer file to get created will be 72,960 KB, which will reduce the amount of space required to create the initial buffer files. But now there's a big problem. In the previous scenario, we were able to greatly benefit from the file size reduction of the initial buffer files that became part of the live TV buffer. Since all initial buffer files were the same size, there was no doubt as to how much free space would be gained as each of the two initial buffer files were transitioned to closed buffer files. However, in this scenario, it is possible for the 72,960 KB initial buffer file to become part of the live TV buffer. If this happens, a significantly less amount of space will be freed when the file is transitioned from an initial buffer file to a closed buffer file. That would be a worst-case scenario; therefore, it must be assumed this file will become part of the live TV buffer. In the previous scenario, the very first step was to add the amount of space occupied by the two initial buffer files that would be used for live TV buffering to the total amount of free space on the drive after all the initial buffer files were created. The same thing must be done in this scenario, but instead of using two files with a size of 510,720 KB, substitute the 72,960 KB file for one of the 510,720 KB files. This works out to 583,680 KB.

Additionally, the amount of free space used as the starting point for the entire calculation must also be reduced. In this scenario, the free space on the drive is below 510,720 before the last initial buffer file is created. This means the starting number for free space should be reduced by the size of the last buffer file created (72,960 KB). Therefore, the starting amount of free space should be 437,759 KB.

The final difference between this scenario and scenario 1 is in the last step. The amount of space required for the inactive tuners has been reduced, so account for the reduction of one file from 510,720 KB to 72,960 KB. That means in the final step, add 2,626,560 KB (2565 MB) instead of 3,064,320 KB (2,992.5 MB).

So just repeat the steps performed in scenario 1 using these new numbers:

Start by assuming the free space is at the upper limit...437,759 KB. Two of the initial buffer files will be used for live TV buffering. Therefore, the space they are taking up on the drive can be added back in to the total free space to calculate how much space is available for the live TV buffer. In this case, that means adding back one 510,720 KB file and one 72,960 KB file, or 583,680 KB. Plugging this information into Formula 1, we get:

((BackingStoreMaxNumBackingFiles + 1) * BackingStoreEachFileDurationSeconds * 2,560 KB) + Next File Filesize <= 1,021,439 KB

The goal in this section is to conserve space, so BackingStoreEachFileDurationSeconds should be as small as possible (5). That yields the following formula:

((BackingStoreMaxNumBackingFiles + 1) * 12,800 KB) + Next File Filesize <= 1,021,439 KB

At this point, simply plug in 72,960 KB for Next File Filesize. This give you the following formula:

((BackingStoreMaxNumBackingFiles + 1) * 12,800 KB) + 72,960 KB <= 1,021,439 KB

Which further yields:

BackingStoreMaxNumBackingFiles <= 74.09

Round the number down to the nearest whole number to determine the maximum value for BackingStoreMaxNumBackingFiles. If the answer is already a whole number, just subtract 1. In this example, there is only enough free space for BackingStoreMaxNumBackingFiles to be any number less than or equal to 74. Going back to Example B from the previous section, the desired value for BackingStoreMaxNumBackingFiles was 108. This drive does not have enough space for 108 files. Therefore, this scenario would not be suitable for the desired buffer. However, if the desired buffer were decreased to 370 seconds (74 * 5), this scenario would work. Adding in the amount of space required for the inactive buffer files, the total amount of space required for this scenario with a 370 second buffer is 3,064,319 KB (2992.5 MB).

Compare scenario 1 to scenario 2: Scenario 2 allows saving 448.75 MB, but reduces the buffer by 2 minutes, 50 seconds.
 * Scenario 1 requires 3441.25 MB of RAM and offers a 9 minute buffer.
 * Scenario 2 requires 2992.5 MB of RAM and offers a 6 minute, 10 second buffer.

Scenario 3
The same logic applied in scenario 2 should be applied to this scenario. This time, there are two 72,960 files. The worst case scenario is both of those files are used for the live TV buffer, so decrease the amount added to the starting free space as if both files are going to be part of the live TV buffer. Also decrease the starting free space and the space required for the inactive buffers accordingly. Then repeat the calculations done in scenario 2 to arrive at the total space required for scenario 3.

Scenarios 4-7
Use the exact same procedures as scenario 3. The only difference between each of these scenarios and scenario 3 is the amount of space required for the initial buffer files.

Scenario 8
Scenario 8? Aren't there only 7 scenarios? Well, yes and no. There are 7 normal scenarios, then there is a "special" scenario that isn't covered yet, but deserves to be covered.

What is the minimum amount of RAM required to make live TV buffering work? This is the question that inspired this wiki, and the wiki wouldn't be complete without an answer.

The answer depends on two things: WMC is hard-coded with certain minimum values for both BackingStoreMaxNumBackingFiles and BackingStoreEachFileDurationSeconds. The minimum allowed value for BackingStoreMaxNumBackingFiles is 4, and the minimum allowed value for BackingStoreEachFileDurationSeconds is 5. Therefore, the smallest possible buffer size is 20 seconds. Plug the numbers into Formula 1 and assume the next file filesize is 72,960 KB:
 * 1) Tthe maximum bitrate of the content. Throughout this wiki, the maximum bitrate used for calculations has been 2,560 KB/s, which is the maximum bitrate allowed in the US. Other countries may have different limitations, but in this scenario, 2,560 KB/s will still be used. Adjust accordingtly.
 * 2) How many tuners are set up in WMC.

((4 + 1) * 5 * 2560) + 72,960 = 136,960 KB (133.75 MB)

Coincidentally, the part of the equation in parenthesis equals exactly 64,000 KB.

The remaining part of the question is how much space is required for the inactive buffer files. That depends on how many tuners are installed in the system. A single tuner system requires no inactive buffer files, but it does require 2 initial buffer files. Remember, WMC will make 2 of those files, and even if both files are 72,960, that means 145,920 KB would be required to make the initial 2 buffer files. So more RAM is required to start WMC than to play back live TV. So it would appear that's how much RAM would be required, but that is not correct. In a single tuner system, after the first initial buffer file is closed, writing is transferred to the second initial buffer file. Since the first buffer file was not reduced completely to 0 KB, there is not enough free space to create the next buffer file (the drive is short by however much space is occupied by the closed buffer file). With a maximum bitrate of 2,560 KB/s, and a 5 second buffer file, the maximum size of the buffer file is 12,800 KB. So an additional 12,800 KB would be required to allow the first next file to be written. This exact same problem will occur again repeatedly until the oldest buffer file is deleted. So for a BackingStoreMaxNumBackingFiles value of 4, the total amount of extra space needed will be 4 * 12,800 KB, which is exactly 50 MB. So in the US, the answer to the question is simply: However much RAM is required to start WMC plus 50 MB

So a single-tuner system requires 192.5 MB of RAM, a 2-tuner system requires 335 MB of RAM, and a 3-tuner system requires 477.5 MB of RAM. A 3-tuner system would max out the ability to take advantage of the 72,960 KB initial buffer size. Therefore, each additional tuner over 3 will require 997.5 MB of RAM. Simply put, the amount of RAM required is determined by the following formula:

((Number of Tuners - 3) * 997.5 MB) + 477.5 MB