ios - Save CMSampleBufferRef Before Sending it to AVAssetWriter -


i trying save samples in didoutputsamplebuffer delegate before writing album avassetwriter.

this code saving samples:

.h file

@property (nonatomic) nsinteger audioindex; @property (nonatomic) nsinteger videoindex;  @property (nonatomic) cmtime starttime;  @property (nonatomic) cfmutablearrayref audiosamples; @property (nonatomic) cfmutablearrayref videosamples; 

.m file

cfretain(samplebuffer); if (cmsamplebufferdataisready(samplebuffer)) {     if (self.videoindex == 0) {         self.starttime = cmsamplebuffergetpresentationtimestamp(samplebuffer);     }     if (bvideo) {         int count = 0;         count = (int)cfarraygetcount(self.videosamples);         if (count >= 1000) {             cfarraysetvalueatindex(self.videosamples, self.videoindex, samplebuffer);         }         else {             cfarrayappendvalue(self.videosamples, samplebuffer);         }         self.videoindex = (self.videoindex + 1) % 1000;     }     else {         int count;         count = (int)cfarraygetcount(self.audiosamples);         if (count >= 1000) {             cfarraysetvalueatindex(self.audiosamples, self.audioindex, samplebuffer);         }         else {             cfarrayappendvalue(self.audiosamples, samplebuffer);         }         self.audioindex = (self.audioindex + 1) % 1000;     } }  cfrelease(samplebuffer); 

the problem last line cfrelease, when calling method cannot use samples later purpose getting error:

[not type retain]: message sent deallocated instance

and when not calling method video images stuck , "not responding" (but instance available).

you seem setting pair of ring buffers audio , video. releasing cmsamplebuffers have placed onto ring buffers - when need access them not there. quick fix release cmsamplebuffer when overwrite on ring buffer new sample. whenever write sample ring buffer, should check whether existing sample overwritten @ index, , cfrelease @ point.

however not going work out breaking cmsamplebuffer memory model result in dropped frames or worse.

here apple docs advise:

if application causing samples dropped retaining provided cmsamplebufferref objects long, needs access sample data long period of time, consider copying data new buffer , releasing sample buffer (if retained) memory references can reused.

wwdc 2011 video 419, capturing camera using av foundation on ios 5 includes, in it's list of do's , dont's (pdf p.50):

  • don't hold on sample buffers outside of data output callbacks
  • don't take long time process buffers in data output callbacks

here example using buffer copying (not mine - haven't tested it). note caveat: "but there problem, memory of super large, video used caution."

you may need rethink design. whatever processing planning ring buffer content, perhaps @ least some of undertaken before adding data ring buffer, not holding onto cmsamplebuffers rather data has been processed (and less memory-intensive).


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -