Skip to content
This repository was archived by the owner on Dec 22, 2020. It is now read-only.
This repository was archived by the owner on Dec 22, 2020. It is now read-only.

App crashing while converting video frames to Images iOS #216

@rohitphogat19

Description

@rohitphogat19

I am using the Agora-Plugin-Raw-Data-API-Objective-C sample to record and save local video during a video call. But it is crashing when converting all stored Video frames to Images. I have used and modify image creation function in AgoraMediaDataPlugin.mm file from the sample. The issue comes when I press the stop recording button and start converting all stored frames to images.

Swift Code:

func mediaDataPlugin(_ mediaDataPlugin: AgoraMediaDataPlugin, didCapturedVideoRawData videoRawData: AgoraVideoRawData) -> AgoraVideoRawData {
        self.videoFrameDatas.append(videoRawData)
        return videoRawData
    }

@objc private func didPressStopRecordingButton() {
        self.agoraKit?.leaveChannel(nil)
        
        for frame in videoFrameDatas {
            if let image = self.agoraMediaDataPlugin?.yuvToUIImage(with: frame) {
                self.recordedImages.append(image)
            }

        }
    }

AgoraMediaDataPlugin.mm code

- (AGImage *)yuvToUIImageWithVideoRawData:(AgoraVideoRawData *)data {
    size_t width = data.width;
    size_t height = data.height;
    size_t yStride = data.yStride;
    size_t uvStride = data.uStride;
    
    char* yBuffer = data.yBuffer;
    char* uBuffer = data.uBuffer;
    char* vBuffer = data.vBuffer;
    
    size_t uvBufferLength = height * uvStride;
    char* uvBuffer = (char *)malloc(uvBufferLength);
    for (size_t uv = 0, u = 0; uv < uvBufferLength; uv += 2, u++) {
        // swtich the location of U、V,to NV12
        uvBuffer[uv] = uBuffer[u];
        uvBuffer[uv+1] = vBuffer[u];
    }
    
    @autoreleasepool {
        void * planeBaseAddress[2] = {yBuffer, uvBuffer};
        size_t planeWidth[2] = {width, width / 2};
        size_t planeHeight[2] = {height, height / 2};
        size_t planeBytesPerRow[2] = {yStride, uvStride * 2};
        
        CVPixelBufferRef pixelBuffer = NULL;
        CVReturn result = CVPixelBufferCreateWithPlanarBytes(kCFAllocatorDefault,
                                                             width, height,
                                                             kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
                                                             NULL, 0,
                                                             2, planeBaseAddress, planeWidth, planeHeight, planeBytesPerRow,
                                                             NULL, NULL, NULL,
                                                             &pixelBuffer);
        if (result != kCVReturnSuccess) {
            NSLog(@"Unable to create cvpixelbuffer %d", result);
        }
        
        AGImage *image = [self CVPixelBufferToImage:pixelBuffer rotation:data.rotation];
        CVPixelBufferRelease(pixelBuffer);
        if(uvBuffer != NULL) {
            free(uvBuffer);
            uvBuffer = NULL;
        }
        return image;
        
    }
}

Crash:

Also, is there any alternatives to save the local video other than using Video Raw data?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions