title: Keeping the last X minutes of a recording video
body: <p>I'm developing an app which requires me to record a lot of video data but I don't need to store them all.
  What I need to do is to keep just the last X minutes of the recorded stream.</p>
  
  <p>This means that I need a way to remove the oldest sample everytime that I need to store a new one.</p>
  
  <p>So I started working with this article: <a href="http://msdn.microsoft.com/library/windowsphone/develop/hh394041%28v=vs.105%29.aspx" rel="nofollow">http://msdn.microsoft.com/library/windowsphone/develop/hh394041%28v=vs.105%29.aspx</a></p>
  
  <p>The first idea that I had was to just call the <code>StopVideoRecording()</code> and then the <code>StartVideoRecording()</code> using a timer each X minutes.</p>
  
  <p>Now, at first this made sense but it won't work.</p>
  
  <p>The problem is that doing this way will delete the previous data each X minutes.</p>
  
  <p>This means that, if we record 12 minutes and we need to keep the last 5, following this idea we'll delete the first 5 and then the second five, leaving just the last 2 minutes and this is not what I was looking for.</p>
  
  <p>I moved then my attention to the <code>VideoSink</code> class because of the <code>OnSample</code> method.</p>
  
  <p>This seems pretty simple, we intercept every sample and we store it in a fixed size byte array (the size depends on the needed length and the sample's size).</p>
  
  <p>When the buffer is full we just shift everything on the left before adding the new sample.
  The problem is that a test video of just 1 minute generated something like 2GB of samples and this makes this way really hard to manage.</p>
  
  <p>I know that those samples are uncompressed, but wouldn't be hard, for a smartphone, to get a sample, compress it, shift a big array, insert the sample and write the array to a file and do it on EVERY sample received?</p>
  
  <p>Yeah, I'm talking about writing the array to a file because we need to persist this video somehow. It may happen that the battery stops working, and having it just in RAM will let us loose everything that we recorded!</p>
  
  <p>The last idea that came to mind was to use a combo of <code>VideoSink</code> and <code>FileSink</code>.
  While the <code>FileSink</code> does the compression magic (I even decompiled this class to understand what it does but there's no code inside!), we use the <code>VideoSink</code>'s <code>OnSample</code> method to manually remove the unneeded data from the mp4 file used by the <code>FileSink</code>.</p>
  
  <p>This one sounds quite hard because I don't know if I can write to the file with both <code>FileSink</code> and <code>VideoSink</code> without concurrency issues, and I've not found a good c# library to help me working with the mp4 files without having to deal with its structure.
  The only library that I found is this one <a href="http://basemedia.codeplex.com/" rel="nofollow">http://basemedia.codeplex.com/</a> but it totally lacks documentation (each link in the documentation page gives a 404 error).</p>
  
  <p>I'm starting to think that this is something that can't be done, but I'd like to see if there's someone here which can point me to the right direction.</p>
  
  <p>EDIT:
  Just to be clear, I used the "recording" word and not the "recorded" one beacuse I'm talking about trimming the video while it's still recording!
  This is not about editing it once it has been saved, but something more like removing stuff from the stream while I'm writing it to disk.</p>