private void OnDestroy()
 {
     if (Parsec != null)
     {
         Parsec.ParsecDestroy();
     }
 }
    private void InitializeParsec()
    {
        Parsec = new Parsec();

        var status = Parsec.Init();

        Log($"Parsec Init: {status}");
    }
    private IEnumerator ClientPollFrame()
    {
        while (true)
        {
            yield return(null);

            Parsec.ClientPollFrame(0, OnClientReceiveFrame, 0u);
        }
    }
Beispiel #4
0
    private void InitParsec()
    {
        parsec = new Parsec();
        Parsec.ParsecStatus status = parsec.Init();
        Debug.Log($"Parsec init: {status}");

        sampleRate = (uint)AudioSettings.outputSampleRate;
        instance   = this;
        //framePCMData = new List<float[]>();

        StartCoroutine(CheckStatus());
    }
    /// <summary>
    /// Starts hosting a Parsec session with the associated session ID.
    /// Streaming will not being until <see cref="StartHostStreamer"/> is called.
    /// </summary>
    /// <param name="sessionId"></param>
    public void Host(string sessionId)
    {
        InitializeParsec();

        var status = Parsec.HostStart(Parsec.ParsecHostMode.HOST_GAME, sessionId);

        Log($"Starting host: {status}");

        hostInput = gameObject.AddComponent <ParsecHostInput>();
        hostInput.Initialize(Parsec);

        StartCoroutine(HostPollEvents());
    }
    private IEnumerator ClientMonitorScreenSize()
    {
        while (true)
        {
            yield return(null);

            if (Screen.width != currentScreenWidth || Screen.height != currentScreenHeight)
            {
                currentScreenWidth  = Screen.width;
                currentScreenHeight = Screen.height;

                Parsec.ClientSetDimensions(0, (uint)currentScreenWidth, (uint)currentScreenHeight, 1);
            }
        }
    }
    private IEnumerator HostSubmitFrame()
    {
        while (true)
        {
            yield return(new WaitForEndOfFrame());

            if (screenshot != null)
            {
                Destroy(screenshot);
            }

            screenshot = ScreenCapture.CaptureScreenshotAsTexture(1);
            UnityNative.UnitySubmitFrame(Parsec.GetPointer(), screenshot.GetNativeTexturePtr());
            GL.IssuePluginEvent(UnityNative.UnityGetRenderEventFunction(), 1);
        }
    }
Beispiel #8
0
    public void Connect(string sessionId, string userId)
    {
        InitializeParsec();

        Parsec.ParsecClientConfig defaultConfig = new Parsec.ParsecClientConfig()
        {
            mediaContainer = 0,
            protocol       = 1,
            secret         = "",
            pngCursor      = false
        };

        var status = Parsec.ClientConnect(defaultConfig, sessionId, userId);

        Log($"Connecting client: {status}");

        gameObject.AddComponent <ParsecGuestInput>().Initialize(Parsec);
    }
Beispiel #9
0
    // Parsec encodes submitted user data from strings into null-terminated UTF-8 bytes.
    public static string HostGetUserDataString(this Parsec parsec, uint key)
    {
        IntPtr ptr = parsec.GetBuffer(key);

        int length = 0;

        while (Marshal.ReadByte(ptr, length) != 0)
        {
            ++length;
        }

        byte[] buffer = new byte[length];
        Marshal.Copy(ptr, buffer, 0, buffer.Length);

        string str = System.Text.Encoding.UTF8.GetString(buffer);

        Parsec.Free(ptr);

        return(str);
    }
    private IEnumerator HostPollEvents()
    {
        while (true)
        {
            yield return(null);

            while (Parsec.HostPollEvents(0u, out Parsec.ParsecHostEvent hostEvent))
            {
                if (hostEvent.type == Parsec.ParsecHostEventType.HOST_EVENT_GUEST_STATE_CHANGE)
                {
                    Parsec.ParsecGuestState state = hostEvent.guestStateChange.guest.state;

                    switch (state)
                    {
                    case Parsec.ParsecGuestState.GUEST_CONNECTED:
                        Log($"Guest ID {hostEvent.guestStateChange.guest.id} connected.");

                        var deviceCollection = hostInput.AddGuest(hostEvent.guestStateChange.guest);
                        OnGuestConnected?.Invoke(hostEvent.guestStateChange.guest, deviceCollection);
                        break;

                    case Parsec.ParsecGuestState.GUEST_DISCONNECTED:
                        Log($"Guest ID {hostEvent.guestStateChange.guest.id} disconnected.");

                        OnGuestDisconnected?.Invoke(hostEvent.guestStateChange.guest);
                        break;

                    default:
                        break;
                    }
                }
                else if (hostEvent.type == Parsec.ParsecHostEventType.HOST_EVENT_USER_DATA)
                {
                    Log($"Received user data from {hostEvent.userData.guest.id}.");

                    string json = Parsec.HostGetUserDataString(hostEvent.userData.key);
                    OnReceiveUserData?.Invoke(hostEvent.userData.guest, json);
                }
            }
        }
    }
    public void Connect(string sessionId, string userId)
    {
        InitializeParsec();

        Parsec.ParsecClientConfig defaultConfig = new Parsec.ParsecClientConfig()
        {
            mediaContainer = 0,
            protocol       = 1,
            secret         = "",
            pngCursor      = false
        };

        var status = Parsec.ClientConnect(defaultConfig, sessionId, userId);

        Log($"Connecting client: {status}");

        gameObject.AddComponent <ParsecGuestInput>().Initialize(Parsec);

        // TODO: This should probably start a coroutine to monitor the connection attempt,
        // firing appropriate events on status change/success/failure.
    }
Beispiel #12
0
 public void Initialize(Parsec parsec)
 {
     this.parsec = parsec;
 }
    public void ClientSendUserData(Color color)
    {
        string json = JsonUtility.ToJson(color);

        Parsec.ClientSendUserData(0, json);
    }