在 iPad 3 上将视频写入文件可以正常工作约 5 秒,然后失败

Writing video to file works correctly for about 5 seconds on iPad 3, then fails

我正在创建这个应用程序,它是一个相机。因为它是为 iOS 9 创建的,所以我必须在旧设备上对其进行测试。在那种情况下,iPad 3. 该应用程序在新的 iPad Pro 9.7 上运行完美,但在 iPad 上一段时间后无法写入视频 3.

发生的情况是,应用程序开始正常写入帧但突然失败。

我正在使用这种方法来存储每一帧:

- (void)writeToFileFrame:(CIImage *) finalImage
          withSampleTime:(CMSampleTimingInfo)sampleTime
                 andSize:(CGSize)size
{

  if (!_assetWriter) {
    if (![self initializeAssetWriter]) {
      return;
    }
  }

  // convert CIImage to CMSampleBufferRef and save
  CGRect extent = [finalImage extent];
  CGFloat width = CGRectGetWidth(extent);
  CGFloat height  = CGRectGetHeight(extent);

  if ((width == 0) && (height == 0)) return;


  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSDictionary dictionary], (id)kCVPixelBufferIOSurfacePropertiesKey,
                           @(YES), kCVPixelBufferCGImageCompatibilityKey,
                           @(YES), kCVPixelBufferCGBitmapContextCompatibilityKey,
                           kCVImageBufferYCbCrMatrix_ITU_R_601_4, kCVImageBufferYCbCrMatrixKey,
                           kCVImageBufferColorPrimaries_ITU_R_709_2, kCVImageBufferColorPrimariesKey, nil];

  // Initialize the video input if this is not done yet
  if (!_readyToWriteVideo) {
    _readyToWriteVideo = [self setupAssetWriterVideoInputWithSize:size];
  }

  CVPixelBufferRef pixelBuffer = NULL;

  CVPixelBufferCreate(kCFAllocatorSystemDefault, width, height, kCVPixelFormatType_32BGRA, (__bridge CFDictionaryRef) options, &pixelBuffer);

  CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
  [_ciContext render:finalImage toCVPixelBuffer:pixelBuffer];
  CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

  CMVideoFormatDescriptionRef videoInfo = NULL;
  CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, &videoInfo);

  CMSampleBufferRef oBuf;
  OSStatus status = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBuffer, true, NULL, NULL, videoInfo, &sampleTime, &oBuf);
  CVPixelBufferRelease(pixelBuffer);
  CFRelease(videoInfo);

  if (status != noErr) {
    NSLog(@"error creating CMSampleBufferCreateForImageBuffer");
    CFRelease(oBuf);
    return;
  }

  // Write video data to file only when all the inputs are ready
  if ([self inputsReadyToWriteToFile]) {
    if (_assetWriter.error) {
      NSLog(@"%@",[_assetWriter.error localizedDescription]);
      return;
    }
    [self writeSampleBuffer:oBuf ofType:AVMediaTypeVideo];
  }

  CFRelease(oBuf);

}


- (void)writeSampleBuffer:(CMSampleBufferRef)sampleBuffer ofType:(NSString *)mediaType
{  
  if ( _assetWriter.status == AVAssetWriterStatusUnknown ) {
    NSLog(@"unknown state");
    // If the asset writer status is unknown, implies writing hasn't started yet, hence start writing with start time as the buffer's presentation timestamp
    if ([_assetWriter startWriting]) {
      [_assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
    } else {
          // error
     }
  }

  if ( _assetWriter.status == AVAssetWriterStatusWriting ) {
    // If the asset writer status is writing, append sample buffer to its corresponding asset writer input
    if (mediaType == AVMediaTypeVideo) {
      if (_assetWriterVideoInput.readyForMoreMediaData) {
        if (![_assetWriterVideoInput appendSampleBuffer:sampleBuffer]) {
          NSLog(@"error: %@", [_assetWriter.error localizedFailureReason]); //Ⓐ
        }
      }
    }
    else if (mediaType == AVMediaTypeAudio) {
      if (_assetWriterAudioInput.readyForMoreMediaData) {
        if (![_assetWriterAudioInput appendSampleBuffer:sampleBuffer]) {
                      // error
        }
      }
    }
  }

  if ( _assetWriter.status == AVAssetWriterStatusFailed ) {
    NSLog(@"error"); 
  }

}

这在 iPad Pro 9.7 上工作正常,但在 iPad 3 上它命中第 Ⓐ 行,在正确写入帧大约 5 秒后惨败。

失败并出现错误 -536870211,这显然是一个未知错误。

我已经检查了应用程序是否存在泄漏,并且有 none。

有什么想法吗?

注意:我现在发现只有当我从后置摄像头 (HD) 写入帧时才会出现问题。如果我切换到前置摄像头 VGA (640x480),它工作正常。所以它与内存分配有关,但我已经用仪器检查过,应用程序使用了大约 12 MB 的内存,这个值或多或少是恒定的。没有泄漏。

那个错误是kIOReturnNoMemory

Memory can't be allocated (0xe00002bd).Value: 0xe00002bd (-536870211)

听起来你用的内存太多了。您需要确保在尽可能短的时间内保留尽可能少的样本缓冲区及其衍生物。

Instruments 应用程序内存和 GPU 工具以及 Xcode 的内存量表也应该有帮助。