tags:

views:

136

answers:

1

I wrote an application for video recording which stores the recording to a file. Currently I am looking for a way to implement some kind of "listener" to listen for file changes. The concept I would like to implement is the following: Check the file on a set period of time (for example one second), extract changes (get the data that was written in the last period) and do something with these data.

I would be very thankful if anyone of you could give any suggestion or point to any useful resource that could help to solve the given problem.

A: 

From a high level: you're going to need an "before" copy of the file and an "after" copy of the file. Once you detect that a change has occured you need to compare the two copies of the file.

The easiest way to do a file-change detection would be to just check the modified timestamp on the file. If you're sure you can loop once per second then just see if the timestamp is >= (now - 1). Otherwise, store the old modified time and check to see if the current modified time is larger.

The tricky part is in doing the comparison between the two versions. There are several algorithms to do binary diffs out there, but they're all going to be slow... like O((n+m) log n) slow. Check out the algorithm that bsdiff uses as it's one of the faster ones out there. It might also be wise to look in to the Android NDK for this. This is one of the few things native C will do better than dalvik/Java.

One way you could speed up the diff process would be to split the file into a bunch of chunks, hash the chunks, then compare the hashes each go around. You'd only need to do binary diffs for the chunks that have different hashes. If you decide to go this route look into checksums.

Good luck... there aren't really any good solutions for this on a mobile device as it's an inherently resource intensive process.

fiXedd
One possible shortcut: If the video-recording program always adds data at the end of the file (as most streaming recorders do), then the "change detection" is much easier - just compare the size of the before and after files (you wouldn't even need multiple copies, just keep track of the previous filesize each time).
Tao
That's a good point.
fiXedd