0

I need to collect video streams from a microscope and used SDL's library to obtain the data frameData. However, I didn't know how to parse the video stream data in frameData to create videos that Unity could use。

public class CameraStreamManager : MonoBehaviour
{


    private Thread cameraThread;
    private bool isRunning = false; 
    private SLcamNet.NetCamera _camera; 
    private Texture2D videoTexture;

    public RawImage videoImage; 

    private List<NetInfo> _infos;
    private CameraInfo _cameraInfo;
    private SDLPanel sdlPanel; 
    private Task _task; 

    private int width = 1920;  
    private int height = 1080;
    private bool frameReady = false;
    public void InitializeCamera()
    {
        SLcamNet.NetCamera.InitNet();
        _infos = SLcamNet.NetCamera.SearchAvailableCameras();
        if (_infos.Count < 1)
        {
            Thread.Sleep(100);
            _infos = SLcamNet.NetCamera.SearchAvailableCameras();
        }

    }

    public void OpenCamera()
    {
        sdlPanel?.Dispose();
        sdlPanel = new SDLPanel
        {
            Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
        };

        Debug.LogError("_infos>>>>  " + _infos.Count);
        if (_infos.Count < 1)
        {
            Thread.Sleep(100);
            _infos = SLcamNet.NetCamera.SearchAvailableCameras();
            return;
        }

        CameraSettings settings = new CameraSettings
        {
            netInfo = _infos[0],
            resolution = new CameraResolution(1920, 1080),
            streamFormat = StreamFormat.kMJPEG,
            pixFormat = PixFormat.kRGBA8888,
            rcMode = "cbr",
            quality = "high",
            frameRate = 25,
            bitrate = 86000,
            keyFrameInterval = 90
        };
        settings.netInfo.Username = "admin";
        settings.netInfo.Password = "123456";

        _camera = SLcamNet.NetCamera.Create(settings.netInfo.Model);
        _camera.SetCameraSettings(settings);
        _camera.Login();
        _cameraInfo = _camera.GetNetCameraAllInfo();
        _camera.Open();

        isRunning = true;
        _task = Task.Run(() => StartCameraCapture());
    }

    private void StartCameraCapture()
    {
        Task.Run(() =>
        {
            Thread.Sleep(500); 
            IntPtr frameDataPtr = IntPtr.Zero;

            while (isRunning)
            {
                try
                {
                    if (!_camera.GetVideoFrameData(ref frameDataPtr))
                        continue;

                    SLcamNet.l_md_data_t frameData = SLcamNet.l_md_data_t.FromIntPtr(frameDataPtr);
                    Debug.LogError("frameData>>>>>" + frameData.p_rgb);



                }
                catch (Exception ex)
                {
                    Debug.LogError($"Error in StartCameraCapture: {ex.Message}");
                }
            }
        });
    }

    private void OnApplicationQuit()
    {
        isRunning = false;
        _task = null;

        if (sdlPanel != null)
        {
            sdlPanel.Dispose(true);
            sdlPanel = null;
        }

        _camera?.Close();
    }
}
3
  • So what exactly is returned by frameData.p_rgb ? Commented Jan 10 at 6:40
  • I'd expect some kind of documentation by the camera/SDL package on this that describes what your data is like, much like Kinect v2's documentation told you (iirc) that e.g. Depth Frame data were given as large array of double format in millimeter values, row major. So you could load it up in a Texture2D for example by converting to a float[] (not performant but just to give an example). But I can't find anything googling "SLcamNet.NetCamera". Can you add a link to the library? I somehow doubt SDL is it. Commented Jan 14 at 12:50
  • I'm sorry for seeing your reply so late. Firstly, SDL refers to Simple DirectMedia Layer. I debugged 'frameData', which is a pointer address that stores video streams. I tried to parse this video stream, but it still failed. I abandoned this plan about a week ago. Thank you again for your friendly response and careful pointing out of key issues. Commented Jan 20 at 7:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.