草庐IT

ios - 带有视频背景和绘画叠加性能问题的全视频屏幕捕获

coder 2023-09-26 原文

长时间的 stackoverflow 阅读器,第一次海报。

我正在尝试创建一个名为 的 iPad 应用程序CloudWriter .该应用程序的概念是绘制您在云中看到的形状。下载应用程序后,启动时 CloudWriter ,用户将看到实时视频背景(来自后置摄像头),其上有一个 OpenGL 绘图层。用户将能够打开应用程序,将 iPad 指向天空中的云彩,然后在显示屏上绘制他们所看到的内容。

该应用程序的一个主要功能是让用户记录 session 期间显示器上发生的事情的视频屏幕截图。实时视频源和“绘图” View 将成为平面(合并)视频。

关于当前如何工作的一些假设和背景信息。

  • 使用 Apples AVCamCaptureManager,这是 AVCam 的一部分示例项目,作为大部分相机相关代码的基础。
  • 使用 初始化 AVCamCapture session AVCaptureSessionPresetMedium 作为预设。
  • 开始通过 videoPreviewLayer 将相机馈送作为背景输出。
  • 使用允许使用 openGL 进行“绘图”(指画风格)的 View 覆盖实时 videoPreviewLayer。 “绘图” View 背景是 [UIColor clearColor]。

  • 在这一点上,我们的想法是用户可以将 iPad3 摄像头指向天空中的一些云彩并绘制他们看到的形状。此功能完美无缺。当我尝试制作用户 session 的“平面”视频屏幕截图时,我开始遇到性能问题。生成的“平面”视频将使相机输入与用户的绘图实时重叠。

    具有与我们正在寻找的功能类似的功能的应用程序的一个很好的例子是 Board Cam ,在 App Store 中可用。

    要启动该过程, View 中始终有一个“记录”按钮可见。当用户点击录制按钮时,期望在再次点击录制按钮之前, session 将被录制为“平面”视频屏幕截图。

    当用户点击“录制”按钮时,代码中会发生以下情况
  • AVCaptureSessionPreset 更改AVCaptureSessionPresetMedium AVCaptureSessionPresetPhoto , 允许访问
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    
  • isRecording 值设置为 .
  • didOutputSampleBuffer 开始获取数据并从当前视频缓冲区数据创建一个图像。它通过调用
    - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
    
  • self.currentImage 设置为此
  • 应用程序根 View Controller 开始覆盖 drawRect 以创建扁平图像,用作最终视频中的单个帧。
  • 该帧被写入平面视频

  • 为了创建一个平面图像,用作单独的帧,在根 ViewController 的 drawRect 函数中,我们抓取 AVCamCaptureManager 的 接收到的最后一帧。 didOutputSampleBuffer 代码。那就是下面
    - (void) drawRect:(CGRect)rect {
    
    
        NSDate* start = [NSDate date];
        CGContextRef context = [self createBitmapContextOfSize:self.frame.size];
    
        //not sure why this is necessary...image renders upside-down and mirrored
        CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height);
        CGContextConcatCTM(context, flipVertical);
    
        if( isRecording)
            [[self.layer presentationLayer] renderInContext:context];
    
        CGImageRef cgImage = CGBitmapContextCreateImage(context);
        UIImage* background = [UIImage imageWithCGImage: cgImage];
        CGImageRelease(cgImage);
    
        UIImage *bottomImage = background;
    
    
        if(((AVCamCaptureManager *)self.captureManager).currentImage != nil && isVideoBGActive )
        {
    
            UIImage *image = [((AVCamCaptureManager *)self.mainContentScreen.captureManager).currentImage retain];//[UIImage
            CGSize newSize = background.size;
            UIGraphicsBeginImageContext( newSize );
            // Use existing opacity as is
            if( isRecording )
            {
                if( [self.mainContentScreen isVideoBGActive] && _recording)
                {
                    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
                }
                // Apply supplied opacity
    
                [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
    
            }
            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
    
            self.currentScreen = newImage;
    
    
    
            [image release];
        }
    
        if (isRecording) {
            float millisElapsed = [[NSDate date] timeIntervalSinceDate:startedAt] * 1000.0;
            [self writeVideoFrameAtTime:CMTimeMake((int)millisElapsed, 1000)];
        }
    
        float processingSeconds = [[NSDate date] timeIntervalSinceDate:start];
        float delayRemaining = (1.0 / self.frameRate) - processingSeconds;
    
        CGContextRelease(context);
    
        //redraw at the specified framerate
        [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:delayRemaining > 0.0 ? delayRemaining : 0.01];  
    }
    

    createBitmapContextOfSize 在下面
    - (CGContextRef) createBitmapContextOfSize:(CGSize) size {
        CGContextRef    context = NULL;
        CGColorSpaceRef colorSpace = nil;
        int             bitmapByteCount;
        int             bitmapBytesPerRow;
    
        bitmapBytesPerRow   = (size.width * 4);
        bitmapByteCount     = (bitmapBytesPerRow * size.height);
        colorSpace = CGColorSpaceCreateDeviceRGB();
        if (bitmapData != NULL) {
            free(bitmapData);
        }
        bitmapData = malloc( bitmapByteCount );
        if (bitmapData == NULL) {
            fprintf (stderr, "Memory not allocated!");
            CGColorSpaceRelease( colorSpace );
            return NULL;
        }
    
        context = CGBitmapContextCreate (bitmapData,
                                         size.width ,
                                         size.height,
                                         8,      // bits per component
                                         bitmapBytesPerRow,
                                         colorSpace,
                                         kCGImageAlphaPremultipliedFirst);
    
        CGContextSetAllowsAntialiasing(context,NO);
        if (context== NULL) {
            free (bitmapData);
            fprintf (stderr, "Context not created!");
            CGColorSpaceRelease( colorSpace );
            return NULL;
        }
    
        //CGAffineTransform transform = CGAffineTransformIdentity;
        //transform = CGAffineTransformScale(transform, size.width * .25, size.height * .25);
        //CGAffineTransformScale(transform, 1024, 768);
    
        CGColorSpaceRelease( colorSpace );
    
        return context;
    }
    

    - (void)captureOutput:didOutputSampleBuffer fromConnection
    // Delegate routine that is called when a sample buffer was written
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection
    {
    
        // Create a UIImage from the sample buffer data
        [self imageFromSampleBuffer:sampleBuffer];
    }
    

    - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 以下
    // Create a UIImage from sample buffer data - modifed not to return a UIImage *, rather store it in self.currentImage
    - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
    {
    
        // unlock the memory, do other stuff, but don't forget:
    
        // Get a CMSampleBuffer's Core Video image buffer for the media data
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        // Lock the base address of the pixel buffer
        CVPixelBufferLockBaseAddress(imageBuffer, 0);
    
       // uint8_t *tmp = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
        int bytes = CVPixelBufferGetBytesPerRow(imageBuffer); // determine number of bytes from height * bytes per row
        //void *baseAddress = malloc(bytes);
        size_t height = CVPixelBufferGetHeight(imageBuffer);     
        uint8_t *baseAddress = malloc( bytes * height );
        memcpy( baseAddress, CVPixelBufferGetBaseAddress(imageBuffer), bytes * height );
        size_t width = CVPixelBufferGetWidth(imageBuffer);
    
        // Create a device-dependent RGB color space
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
        // Create a bitmap graphics context with the sample buffer data
        CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
                                                     bytes, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
    
    
        // CGContextScaleCTM(context, 0.25, 0.25); //scale down to size
        // Create a Quartz image from the pixel data in the bitmap graphics context
        CGImageRef quartzImage = CGBitmapContextCreateImage(context);
        // Unlock the pixel buffer
        CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    
        // Free up the context and color space
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        free(baseAddress);
    
        self.currentImage = [UIImage imageWithCGImage:quartzImage scale:0.25 orientation:UIImageOrientationUp];
    
        // Release the Quartz image
        CGImageRelease(quartzImage);
    
    
        return nil; 
    }
    

    最后,我使用 将其写入磁盘writeVideoFrameAtTime:CMTimeMake ,代码如下:
    -(void) writeVideoFrameAtTime:(CMTime)time {
        if (![videoWriterInput isReadyForMoreMediaData]) {
            NSLog(@"Not ready for video data");
        }
        else {
            @synchronized (self) {
                UIImage* newFrame = [self.currentScreen retain];
                CVPixelBufferRef pixelBuffer = NULL;
                CGImageRef cgImage = CGImageCreateCopy([newFrame CGImage]);
                CFDataRef image = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
    
                if( image == nil )
                {
                    [newFrame release];
                    CVPixelBufferRelease( pixelBuffer );
                    CGImageRelease(cgImage);
                    return;
                }
    
                int status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, avAdaptor.pixelBufferPool, &pixelBuffer);
                if(status != 0){
                    //could not get a buffer from the pool
                    NSLog(@"Error creating pixel buffer:  status=%d", status);
                }
    
                // set image data into pixel buffer
                CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
                uint8_t* destPixels = CVPixelBufferGetBaseAddress(pixelBuffer);
                CFDataGetBytes(image, CFRangeMake(0, CFDataGetLength(image)), destPixels);  //XXX:  will work if the pixel buffer is contiguous and has the same bytesPerRow as the input data
    
                if(status == 0){
                    BOOL success = [avAdaptor appendPixelBuffer:pixelBuffer withPresentationTime:time];
                    if (!success)
                        NSLog(@"Warning:  Unable to write buffer to video");
                }
    
                //clean up
                [newFrame release];
                CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );
                CVPixelBufferRelease( pixelBuffer );
                CFRelease(image);
                CGImageRelease(cgImage);
            }
    
        }
    
    }
    

    一旦 isRecording 设置为 YES,iPad 3 上的性能就会从大约 20FPS 上升到 5FPS。使用 仪器 ,我能够看到以下代码块(来自 drawRect: )导致性能下降到无法使用的水平。
     if( _recording )
            {
                if( [self.mainContentScreen isVideoBGActive] && _recording)
                {
                    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
                }
                // Apply supplied opacity
    
                [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
    
            }
    

    我的理解是,因为我正在捕获全屏,我们失去了“drawInRect”应该提供的所有好处。具体来说,我说的是更快的重绘,因为理论上,我们只更新显示的一小部分(传入 CGRect)。同样,捕获全屏,我不确定 drawInRect 可以提供几乎一样多的好处。

    为了提高性能,我想如果我要缩小图像 imageFromSampleBuffer 提供和绘图 View 的当前上下文,我会看到帧速率的增加。不幸的是,CoreGrapics.Framework 不是我过去使用过的东西,所以我不知道我是否能够有效地将性能调整到可接受的水平。

    任何 CoreGraphics Guru 有输入吗?

    此外,某些代码的 ARC 已关闭,分析器显示一个泄漏,但我认为这是误报。

    即将推出, CloudWriter ™,其中天空的限制!

    最佳答案

    如果您想要不错的录制性能,您将需要避免使用 Core Graphics 重新绘制内容。坚持使用纯 OpenGL ES。

    您说您已经在 OpenGL ES 中完成了手指绘画,因此您应该能够将其渲染为纹理。实时视频源也可以指向纹理。从那里,您可以根据手指绘画纹理中的 alpha channel 对两者进行叠加混合。

    使用 OpenGL ES 2.0 着色器很容易做到这一点。其实我的GPUImage如果您从绘画代码提供渲染的纹理,开源框架可以处理视频捕获和混合部分(请参阅 FilterShowcase 示例应用程序以获取覆盖在视频上的图像示例)。您必须确保绘画使用的是 OpenGL ES 2.0,而不是 1.1,并且它与 GPUImage OpenGL ES 上下文具有相同的共享组,但我将在 CubeExample 应用程序中展示如何做到这一点。

    我还通过使用可用的纹理缓存(在 iOS 5.0+ 上)以高性能方式在 GPUImage 中为您处理视频录制。

    通过使用类似我的框架并留在 OpenGL ES 中,您应该能够以稳定的 30 FPS 为 720p 视频(iPad 2)或 1080p 视频(iPad 3)录制这种混合。

    关于ios - 带有视频背景和绘画叠加性能问题的全视频屏幕捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12938545/

    有关ios - 带有视频背景和绘画叠加性能问题的全视频屏幕捕获的更多相关文章

    1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

      这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

    2. Ruby 文件 IO 定界符? - 2

      我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

    3. 屏幕录制为什么没声音?检查这2项,轻松解决 - 2

      相信很多人在录制视频的时候都会遇到各种各样的问题,比如录制的视频没有声音。屏幕录制为什么没声音?今天小编就和大家分享一下如何录制音画同步视频的具体操作方法。如果你有录制的视频没有声音,你可以试试这个方法。 一、检查是否打开电脑系统声音相信很多小伙伴在录制视频后会发现录制的视频没有声音,屏幕录制为什么没声音?如果当时没有打开音频录制,则录制好的视频是没有声音的。因此,建议在录制前进行检查。屏幕上没有声音,很可能是因为你的电脑系统的声音被禁止了。您只需打开电脑系统的声音,即可录制音频和图画同步视频。操作方法:步骤1:点击电脑屏幕右下侧的“小喇叭”图案,在上方的选项中,选择“声音”。 步骤2:在“声

    4. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

      1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

    5. 动漫制作技巧如何制作动漫视频 - 2

      动漫制作技巧是很多新人想了解的问题,今天小编就来解答与大家分享一下动漫制作流程,为了帮助有兴趣的同学理解,大多数人会选择动漫培训机构,那么今天小编就带大家来看看动漫制作要掌握哪些技巧?一、动漫作品首先完成草图设计和原型制作。设计草图要有目的、有对象、有步骤、要形象、要简单、符合实际。设计图要一致性,以保证制作的顺利进行。二、原型制作是根据设计图纸和制作材料,可以是手绘也可以是3d软件创建。在此步骤中,要注意的问题是色彩和平面布局。三、动漫制作制作完成后,加工成型。完成不同的表现形式后,就要对设计稿进行加工处理,使加工的难易度降低,并得到一些基本准确的概念,以便于后续的大样、准确的尺寸制定。四、

    6. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

      2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

    7. TimeSformer:抛弃CNN的Transformer视频理解框架 - 2

      Transformers开始在视频识别领域的“猪突猛进”,各种改进和魔改层出不穷。由此作者将开启VideoTransformer系列的讲解,本篇主要介绍了FBAI团队的TimeSformer,这也是第一篇使用纯Transformer结构在视频识别上的文章。如果觉得有用,就请点赞、收藏、关注!paper:https://arxiv.org/abs/2102.05095code(offical):https://github.com/facebookresearch/TimeSformeraccept:ICML2021author:FacebookAI一、前言Transformers(VIT)在图

    8. ruby - 如何让Ruby捕获线程中的语法错误 - 2

      我正在尝试使用ruby​​编写一个双线程客户端,一个线程从套接字读取数据并将其打印出来,另一个线程读取本地数据并将其发送到远程服务器。我发现的问题是Ruby似乎无法捕获线程内的错误,这是一个示例:#!/usr/bin/rubyThread.new{loop{$stdout.puts"hi"abc.putsefsleep1}}loop{sleep1}显然,如果我在线程外键入abc.putsef,代码将永远不会运行,因为Ruby将报告“undefinedvariableabc”。但是,如果它在一个线程内,则没有错误报告。我的问题是,如何让Ruby捕获这样的错误?或者至少,报告线程中的错误?

    9. ruby-on-rails - 带有 Zeus 的 RSpec 3.1,我应该在 spec_helper 中要求 'rspec/rails' 吗? - 2

      使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做

    10. ruby-on-rails - 无法在 Rails 助手中捕获 block 的输出 - 2

      我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e

    随机推荐