1

I'm using NSFileHandle to get the data of a video as its being recorded.

It works fine notification-wise and I'm getting notified using NSFileHandleDataAvailableNotification. The problem is the video file eventually doesn't work.

Everytime when comparing the original file with the file created using NSFileHandle data, there are always just several bytes being wrong , meaning NSFileHandle reads them incorrectly.

This is how I append the data

-(void) gotData: (NSNotification *) not{
    NSFileHandle *handle    = not.object;
    NSData *data            = [handle availableData];

    if(data.length){
        NSLog(@"got %d", data.length);
        [test appendData: data];
        [handle waitForDataInBackgroundAndNotify];
    }else{
        NSLog(@"Ended");
        [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleDataAvailableNotification object: handle];
    }
}

This is how I'm writing the data eventually :

[test writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"x.mp4"] atomically:YES];

And when doing a diff between the original file and the one from NSFileHandle, here are the bytes wrong (even though both are the exact same size): enter image description here

I'm really clueless about this stranger behaviour, and if you ever got stuck with a similar issue I'd love your help on this.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83

1 Answers1

0

I'm gonna go out on a limb here and say there is no way that NSFileHandle is giving you incorrect data. What I think is happening is that the data is actually being changed (by the program that is writing it out) after you have gotten the data.

I don't know what you are trying to accomplish exactly so I cant suggest a better approach.

I have some similar code that reads data from a file that is being written out by another process, only mine uses readabilityHandler instead of NSFileHandleDataAvailableNotification. I dont have much reason to believe that this would fix your problem, but it is wort a try. I know that you can run into threading issues with NSNotification so that might be something to look at.

You maybe should use [NSFileHandle offsetInFile] (and compare with your expected location based on data received) to check if things are getting rewound or something.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • I'm pretty sure that's exactly what's happening, because i'm reading the file as its being written, and when it ends, I'm writing both the regular data and the one read from NSFileHandle and only then compare them. It doesn't make much sense the file would be changed in the middle of it and just for these 5-6 bytes... – Shai Mishali Aug 02 '14 at 08:36
  • @ShaiMishali if its just changing for 5 or 6 bytes then I am left to infer that these bytes are changing because they represent the *length* of the (actual) video data (which goes up over time). – Brad Allred Aug 02 '14 at 14:15
  • As the video gets longer, more bytes change... I don't think it has anything to do with the length because those bytes contain no numerical values... Only thing I can think of is EXIF ... – Shai Mishali Aug 03 '14 at 13:20
  • @ShaiMishali you are probably right since I see now that the `moov` identifier is getting changed. Updating my answer with more ideas. – Brad Allred Aug 04 '14 at 03:00