0 Replies
      Latest reply on Dec 22, 2019 6:27 AM by yasuoka
      yasuoka Level 1 Level 1 (0 points)

        Error exceeding memory allowance when processing CMSampleBuffer to NSData conversion in RPBroadcastSampleHandler.

        Error code: Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)

         

         

        I am processing the function of converting a large 1920x1440 image into a binary to send to the socket server.

        I tried resizing to 1200x900 but nothing worked.

         

         

        I have spent a week dealing with this problem, but to no avail.

        I have tried the following:

        Resize image with instructions from here: [https://nshipster.com/image-resizing/][1]

         

         

        1. Get the UIImage object from SampleBuffer and then convert it to jpegData with the lowest compression quality.

            func getDataFromCMSampleBuffer (sampleBuffer: CMSampleBuffer) -> Data? {
                if CMSampleBufferDataIsReady (sampleBuffer),
                    let pixelBuffer = CMSampleBufferGetImageBuffer (sampleBuffer) {
                    let ciImage = CIImage (cvImageBuffer: pixelBuffer)
                    let image = UIImage (ciImage: ciImage)
                    return (image.jpegData (compressionQuality: 0.0)) // Error Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
                }
                return nil
            }

         

        2. Resize the UIImage object from CMSampleBuffer before convert to NSData

            let renderer = UIGraphicsImageRenderer (size: CGSize (width: 1200, height: 900))
            return renderer.image {(context) in
                image.draw (in: CGRect (origin: .zero, size: size)) // Error Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
            }

        Or

            UIGraphicsBeginImageContextWithOptions (CGSizeMake (newWidth, newHeight), NO, 0);
            [sourceImage drawInRect: CGRectMake (0, 0, 1200, 900)]; //// Error Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
            UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
            UIGraphicsEndImageContext ();

        3. Get the Image bitmap from CMSampleBuffer before convert to NSData

            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
            CGContextRef context = CGBitmapContextCreate (rgbBuffer, width, height, 8, width * bytesPerPixel, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
            CGImageRef quartzImage = CGBitmapContextCreateImage (context); // Thread 7 error: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
            UIImage * image = [UIImage imageWithCGImage: quartzImage];

        4. Scale CIImage before convert to NSData

            CIFilter *filter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
            [filter setValue:image forKey:kCIInputImageKey];
            [filter setValue:[NSNumber numberWithDouble:0.2] forKey:kCIInputScaleKey];
            [filter setValue:@1.0 forKey:kCIInputAspectRatioKey];
            CIImage *img = (CIImage*)[filter valueForKey:kCIOutputImageKey];
        ...
            CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer: @NO}];
            CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
            [context render:img toBitmap:rawData rowBytes:width*kComponentsPerPixel bounds:img.extent format:kCIFormatRGBA8 colorSpace:rgb];// Thread 7 error: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
            CGDataProviderRef provider = CGDataProviderCreateWithData(nil, rawData, memsize, releasePixels);

         

        Is there a way to convert from CMSampleBuffer to NSData image without exceeding 50MB of memory and the image quality is not reduced too much.

        Or is there any way to resize large images that don't use more than 50MB of memory?

         

         

        Any advice I give is very thankful.

         

         

         

         

          [1]: https://nshipster.com/image-resizing/