ios - 在 iOS 8 中屏幕录制视频崩溃

标签 ios video ios8 ios9 video-capture

这是我的代码的描述

主要是crash上这两条线。

BOOL 成功 = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
如果(!成功){
NSLog(@"警告:无法将缓冲区写入视频");
}

if (dispatch_semaphore_wait(_frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0) {
    return;
}
dispatch_async(_render_queue, ^{
    if (![_videoWriterInput isReadyForMoreMediaData]) return;

    if (!self.firstTimeStamp) {
        self.firstTimeStamp = _displayLink.timestamp;
    }
    CFTimeInterval elapsed = (_displayLink.timestamp - self.firstTimeStamp);
    CMTime time = CMTimeMakeWithSeconds(elapsed, 1000);

    CVPixelBufferRef pixelBuffer = NULL;
    CGContextRef bitmapContext = [self createPixelBufferAndBitmapContext:&pixelBuffer];

    if (self.delegate) {
        [self.delegate writeBackgroundFrameInContext:&bitmapContext];
    }
    // draw each window into the context (other windows include UIKeyboard, UIAlert)
    // FIX: UIKeyboard is currently only rendered correctly in portrait orientation
    dispatch_sync(dispatch_get_main_queue(), ^{
        UIGraphicsPushContext(bitmapContext); {
            for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
                [window drawViewHierarchyInRect:CGRectMake(0, 0, _viewSize.width, _viewSize.height) afterScreenUpdates:NO];
            }
        } UIGraphicsPopContext();
    });

    // append pixelBuffer on a async dispatch_queue, the next frame is rendered whilst this one appends
    // must not overwhelm the queue with pixelBuffers, therefore:
    // check if _append_pixelBuffer_queue is ready
    // if it’s not ready, release pixelBuffer and bitmapContext
    if (dispatch_semaphore_wait(_pixelAppendSemaphore, DISPATCH_TIME_NOW) == 0) {
        dispatch_async(_append_pixelBuffer_queue, ^{
            **BOOL success = [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
            if (!success) {
                NSLog(@"Warning: Unable to write buffer to video");
            }**
            CGContextRelease(bitmapContext);
            CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
            CVPixelBufferRelease(pixelBuffer);

            dispatch_semaphore_signal(_pixelAppendSemaphore);
        });
    } else {
        CGContextRelease(bitmapContext);
        CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
        CVPixelBufferRelease(pixelBuffer);
    }

    dispatch_semaphore_signal(_frameRenderingSemaphore);
});

错误描述也是

thread #34: tid = 0x4aefc, 0x00000001958952bc libsystem_platform.dylib_platform_memmove + 108, queue = 'ASScreenRecorder.append_queue', stop reason = EXC_BAD_ACCESS (code=1, address=0x109c60000) frame #0: 0x00000001958952bc libsystem_platform.dylib_platform_memmove + 108 frame #1: 0x0000000183c32004 CoreMediaFigNEAtomWriterAppendData + 92. frame #2: 0x0000000183c31f8c CoreMediasbufAtom_appendAtomWithMemoryBlock + 104 frame #3: 0x0000000183c2f78c CoreMediasbufAtom_createSerializedDataForPixelBuffer + 588 frame #4: 0x0000000183c2f4b8 CoreMediaFigRemote_CreateSerializedAtomDataForPixelBuffer + 288 frame #5: 0x0000000185a6af3c MediaToolboxremoteWriter_AddPixelBuffer + 140 frame #6: 0x0000000181bcebd4 AVFoundation-[AVFigAssetWriterTrack addPixelBuffer:atPresentationTime:error:] + 176 frame #7: 0x0000000181bca848 AVFoundation-[AVAssetWriterInputWritingHelper appendPixelBuffer:withPresentationTime:] + 124 frame #8: 0x0000000181bc82fc AVFoundation-[AVAssetWriterInput _appendPixelBuffer:withPresentationTime:] + 88 frame #9: 0x0000000181bcdb40 AVFoundation-[AVAssetWriterInputPixelBufferAdaptor appendPixelBuffer:withPresentationTime:] + 104 frame #10: 0x00000001000f45e0 Island Guide Aruba__35-[ASScreenRecorder writeVideoFrame]_block_invoke192(.block_descriptor=) + 132 at ASScreenRecorder.m:301 frame #11: 0x00000001005b0fd4 libdispatch.dylib_dispatch_call_block_and_release + 24 frame #12: 0x00000001005b0f94 libdispatch.dylib_dispatch_client_callout + 16 frame #13: 0x00000001005bbdb8 libdispatch.dylib_dispatch_queue_drain + 780 frame #14: 0x00000001005b42c4 libdispatch.dylib_dispatch_queue_invoke + 132 frame #15: 0x00000001005be5d4 libdispatch.dylib_dispatch_root_queue_drain + 772 frame #16: 0x00000001005c0248 libdispatch.dylib_dispatch_worker_thread3 + 132 frame #17: 0x000000019589d21c libsystem_pthread.dylib`_pthread_wqthread + 816



==================================================== =======================

最佳答案

我假设您正在使用 ASScreenRecorder (因为几行与该 repo 相同)。所以我花了几天时间处理这个崩溃,可能发现了问题,创建像素缓冲池有问题(在 [self createPixelBufferAndBitmapContext:&pixelBuffer]; 方法中)。我修改的代码:

- (void)writeVideoFrame{
  dispatch_async(_render_queue, ^{
    if (![_videoWriterInput isReadyForMoreMediaData]){
      return;
    }

    CVPixelBufferRef pixelBuffer = NULL;
    CVPixelBufferCreate(kCFAllocatorDefault, _viewSize.width, _viewSize.height, kCVPixelFormatType_32BGRA, (__bridge CFDictionaryRef _Nullable)(_outputBufferPoolAuxAttributes), &pixelBuffer);
    CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

    //creating bitmap context
    CGContextRef bitmapContext = NULL;
    bitmapContext = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(pixelBuffer),
                                          CVPixelBufferGetWidth(pixelBuffer),
                                          CVPixelBufferGetHeight(pixelBuffer),
                                          8, CVPixelBufferGetBytesPerRow(pixelBuffer), _rgbColorSpace,
                                          kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst
                                          );

    CGContextScaleCTM(bitmapContext, _scale, _scale);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, _viewSize.height);
    CGContextConcatCTM(bitmapContext, flipVertical);

    dispatch_sync(dispatch_get_main_queue(), ^{
      UIGraphicsPushContext(bitmapContext); {
        [[UIApplication sharedApplication].keyWindow drawViewHierarchyInRect:CGRectMake(0, 0, _viewSize.width, _viewSize.height) afterScreenUpdates:NO];
      } UIGraphicsPopContext();
    });

    CMTime time = CMTimeAdd(_firstAudioTimeStamp, CMTimeMakeWithSeconds([[NSDate date] timeIntervalSinceDate:_startedAt], _firstAudioTimeStamp.timescale));
    [_avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];

    CGContextRelease(bitmapContext);
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    CVPixelBufferRelease(pixelBuffer);
  });
}

如您所见,我不使用 CVPixelBufferPoolRefCVPixelBufferPoolCreatePixelBuffer , 只需创建 CVPixelBufferRef每次与 CVPixelBufferCreate方法。它可能更慢,但自从修改它后,我什至没有崩溃过。

关于ios - 在 iOS 8 中屏幕录制视频崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086827/

相关文章:

php - 如何在 Swift iOS 中向 php 服务器发出 HTTP 请求

video - 我应该在jar文件的manifest.mf文件中给出哪个主类,该jar文件中有一个没有主类的applet?

ios - AssumeRole - AWS iOS SDK 示例

iPhone 应用程序添加新的输入行

iphone - 在 iPhone OS 中加密 SQLite 数据库文件

android - 全宽视频不工作

video - Windows Media Foundation 使用 IMFTransform 将 mp4 电影帧解码为 2D 纹理

ios - 模态 Segue 的表单不适用于 Storyboard

swift - 使用 NSCoding 保存数组

ios - MapKit Tile Overlay 在 iOS8 中损坏