Sebastian Fals
Sebastian Fals

Reputation: 1

VLCException: Failed to perform instanciation on the native side

VLCException: Failed to perform instanciation on the native side. Make sure you installed the correct VideoLAN.LibVLC.[YourPlatform] package in your platform specific project
LibVLCSharp.Shared.Internal.OnNativeInstanciationError () (at <c143e631d5a54a7ca1a10155c09a821c>:0)
LibVLCSharp.Shared.Internal..ctor (System.Func`1[TResult] create, System.Action`1[T] release) (at <c143e631d5a54a7ca1a10155c09a821c>:0)
LibVLCSharp.Shared.LibVLC..ctor (System.String[] options) (at <c143e631d5a54a7ca1a10155c09a821c>:0)
CameraLogic.Start () (at Assets/Scripts/Cameras/CameraLogic.cs:28)

I´m trying to make this on an unity project, i´ve imported all the dlls of libvlc, libvlccore, an libvlcsharp, but it returns me this error, and i dont know how to solve it (Without installing vlc-unity asset).

I need this for running an rtsp server for an interface of cameras. This is my code:

using System;
using System.Runtime.InteropServices;
using UnityEngine;
using LibVLCSharp.Shared;

public class CameraLogic : MonoBehaviour
{
    public Material screenMat;
    public GameObject screen;
    public float changeSpeed = 1.0f; // Velocidad de cambio de textura (segundos por cambio)
    public bool hasSignal;
    public Texture2D[] noSignalFrames;
    private float timer = 0.0f;
    private int currentIndex = 0;
    public int id = 0;
    public string nombre = "No Signal";
    public string cameraID;
    public Texture2D cameraFrame;

    private Material selfMat;
    private LibVLC _libVLC;
    private MediaPlayer _mediaPlayer;
    private GCHandle _gcHandle;

    private void Start()
    { 
        // Opciones para LibVLC
        _libVLC = new LibVLC();
        _mediaPlayer = new MediaPlayer(_libVLC);

        // Inicializar la textura de la cámara
        cameraFrame = new Texture2D(640, 480, TextureFormat.RGBA32, false);
        selfMat = new Material(screenMat);

        // Asignar el material al objeto de la pantalla
        if (screen != null)
        {
            screen.GetComponent<Renderer>().material = selfMat;
        }
        else
        {
            Debug.LogError("El objeto 'screen' no está asignado.");
        }

        // Configurar VLC para renderizar en la textura de Unity
        _mediaPlayer.SetVideoFormat("RV32", (uint)cameraFrame.width, (uint)cameraFrame.height, (uint)(cameraFrame.width * 4));
        _gcHandle = GCHandle.Alloc(cameraFrame.GetPixels32(), GCHandleType.Pinned);

        _mediaPlayer.SetVideoCallbacks(
            (IntPtr opaque, IntPtr planes) =>
            {
                planes = _gcHandle.AddrOfPinnedObject();
                return IntPtr.Zero;
            },
            (IntPtr opaque, IntPtr picture, IntPtr planes) =>
            {
                // Puedes añadir código adicional si necesitas procesar algo al desbloquear
            },
            (IntPtr opaque, IntPtr picture) =>
            {
                if (cameraFrame != null)
                {
                    cameraFrame.Apply();
                }
                else
                {
                    Debug.LogError("cameraFrame es null.");
                }
            }
        );

        var media = new Media(_libVLC, "Here is the rtsp link");
        _mediaPlayer.Play(media);
    }

    void Update()
    {
        if (_mediaPlayer == null)
        {
            hasSignal = false;
            Debug.LogError("MediaPlayer no está inicializado.");
            return;
        }

        if (cameraFrame == null)
        {
            Debug.LogError("cameraFrame no está inicializado.");
            return;
        }

        if (!_mediaPlayer.IsPlaying)
        {
            hasSignal = false;
            timer += Time.deltaTime;

            if (timer >= changeSpeed)
            {
                ShowNoSignal();
                timer = 0.0f;
            }
        }
        else
        {
            hasSignal = true;
            PrintImage();
        }
        if (!hasSignal)
        {
            ShowNoSignal();
        }
    }

    void ShowNoSignal()
    {
        if (noSignalFrames.Length > 0)
        {
            selfMat.mainTexture = noSignalFrames[currentIndex];
            selfMat.SetTexture("_EmissionMap", noSignalFrames[currentIndex]);
            currentIndex = (currentIndex + 1) % noSignalFrames.Length;
        }
        else
        {
            Debug.LogError("No hay marcos de 'no signal' disponibles.");
        }
    }

    void PrintImage()
    {
        if (selfMat != null)
        {
            selfMat.mainTexture = cameraFrame;
            selfMat.SetTexture("_EmissionMap", cameraFrame);
        }
        else
        {
            Debug.LogError("selfMat no está inicializado.");
        }
    }

    private void OnDisable()
    {
        if (_gcHandle.IsAllocated)
        {
            _gcHandle.Free();
        }
        _mediaPlayer?.Stop();
        _mediaPlayer?.Dispose();
        _libVLC?.Dispose();
    }
}

Upvotes: 0

Views: 107

Answers (1)

cube45
cube45

Reputation: 4049

You need vlc-unity as it is the package that allows you to expose the VLC video as a texture.

The vlc-unity asset also takes care of putting the library at the right place, which you likely haven't.

Upvotes: 0

Related Questions