Skip to content

Deep Cloning Frameset #8

Description

@DudusB

Hi,
I am facing a problem when trying to make a deep clone of a frameset.
Goal: copy full frameset from cameras buffer for further processing.
Context: this is a minimal example for copying of framrset and color frame, but I assume IR and Depth should work analogically. Frameset is retrived in external sync mode, where all frames in set are synchronized and required (so the frameset does not contain any null frame when color, depth and ir requested).

Problem: when cloned ion this way, still the new copied frame contains some reference to original frame, however I can not find at which point it happens. I belive it some internall problem. Nice to have would be to get out of box deepclone method for each type of frame/frameset.

#define CREATE_STREAM_PROFILE
#define COPY_META
#define COPY_INFO
#define COPY_DATA
#define COLOR
namespace SDK.OrbbecSDKWrapper.Utils;

public static class FramesetDeepCloneHandler
{
    [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
    public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);

    public static Frameset DeepClone(this Frameset originalFrameset)
    {
        Frameset frameset = null!;
        ColorFrame colorframe = null!;
        DepthFrame depthframe = null!;
        IRFrame irframe = null!;

        StreamProfile colorframeStreamProfile = null!;
        try
        {
            if (null == originalFrameset)
            {
                return null;
            }


            ///Frameset
            ///
            frameset = originalFrameset;

            Format framesetFormat = frameset.GetFormat();
            FrameType framesetType = frameset.GetFrameType();
            var framesetDataSize = frameset.GetDataSize();


            Frameset createdFrameset = Frameset.CreateFrameset();
#if COPY_DATA
            IntPtr framesetDataPtr = frameset.GetDataPtr();
            IntPtr framesetDataCopyPtr = Marshal.AllocHGlobal((int)framesetDataSize);
            memcpy(framesetDataCopyPtr, framesetDataPtr, new UIntPtr(framesetDataSize));

            createdFrameset.UpdateData(framesetDataCopyPtr, framesetDataSize);
            Marshal.FreeHGlobal(framesetDataCopyPtr);

#endif
#if COPY_META
            var framesetMetadataSize = frameset.GetMetadataSize();
            IntPtr framesetMetadataPtr = frameset.GetMetadataPtr();
            IntPtr framesetMetadataCopyPtr = Marshal.AllocHGlobal((int)framesetMetadataSize);
            memcpy(framesetMetadataCopyPtr, framesetMetadataPtr, new UIntPtr(framesetMetadataSize));

            createdFrameset.UpdateMetaData(framesetMetadataCopyPtr, framesetMetadataSize);
            Marshal.FreeHGlobal(framesetMetadataCopyPtr);
#endif
#if COPY_INFO
            frameset.CopyInfo(createdFrameset);
#endif
#if COLOR
            ///Color
            ///

            colorframe = frameset.GetColorFrame(); 
            Format colorframeFormat = colorframe.GetFormat();
            FrameType colorframeType = colorframe.GetFrameType();
            var colorframeDataSize = colorframe.GetDataSize();

            Frame createdColorframe = Frame.Create(colorframeType, colorframeFormat, colorframeDataSize);
#if COPY_DATA
            IntPtr colorframeDataPtr = colorframe.GetDataPtr();
            IntPtr colorframedataCopyPtr = Marshal.AllocHGlobal((int)colorframeDataSize);
            memcpy(colorframedataCopyPtr, colorframeDataPtr, new UIntPtr(colorframeDataSize));

            createdColorframe.UpdateData(colorframedataCopyPtr, colorframeDataSize);
            Marshal.FreeHGlobal(colorframedataCopyPtr);
#endif
#if COPY_META
            var colorframeMetadataSize = colorframe.GetMetadataSize();
            IntPtr colorframeMetadataPtr = colorframe.GetMetadataPtr();
            IntPtr colorframeMetadataCopyPtr = Marshal.AllocHGlobal((int)colorframeMetadataSize);
            memcpy(colorframeMetadataCopyPtr, colorframeMetadataPtr, new UIntPtr(colorframeMetadataSize));

            createdColorframe.UpdateMetaData(colorframeMetadataCopyPtr, colorframeMetadataSize);
            Marshal.FreeHGlobal(colorframeMetadataCopyPtr);
#endif
#if COPY_INFO
            colorframe.CopyInfo(createdColorframe);
#endif

#if CREATE_STREAM_PROFILE
            colorframeStreamProfile = colorframe.GetStreamProfile(); 
            var colorframeStreamType = colorframeStreamProfile.GetStreamType();
            var colorframeStreamFormat = colorframeStreamProfile.GetFormat();
            var colorframeWidth = colorframe.GetWidth();
            var colorframeHeight = colorframe.GetHeight();
            uint colorframeFPS = 30;
            var createdColorframeVideoStreamProfile = VideoStreamProfile.Create(colorframeStreamType, colorframeStreamFormat, colorframeWidth, colorframeHeight, colorframeFPS);
            createdColorframe.SetStreamProfile(createdColorframeVideoStreamProfile);
            createdColorframeVideoStreamProfile.Dispose();
#endif

            createdFrameset.PushFrame(createdColorframe);
            createdColorframe.Dispose();
#endif

            return createdFrameset;
        }
        catch (Exception e)
        {
            Console.WriteLine($"{e?.Message} | {e?.InnerException?.Message}");
            throw;
        }
        finally
        {
            colorframeStreamProfile?.Dispose();
            colorframe?.Dispose();
            frameset?.Dispose();
        }
    }

}

I appreciate any help or suggestions how to solve the problem and get the funtionality I need.

Thank you!

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