Exemple #1
0
 public void RemoveClientAndStopDoingAnything(OSCPacket packet)
 {
     if (m_client != null)
     {
         m_client.Close();
         m_client = null;
     }
     StopDoingAnything();
 }
Exemple #2
0
    private void Test()
    {
        client = new OSCClient(IPAddress.Parse("127.0.0.1"), listenPort);
        OSCMessage m = new OSCMessage("/testMessage");

        client.Send(m);
        client.Flush();
        client.Close();
        client = null;
    }
Exemple #3
0
    public static void SendMessage(OSCMessage m, string host, int port)
    {
        if (Instance.LogOutgoing)
        {
            string args = "";
            for (int i = 0; i < m.Data.Count; i++)
            {
                args += (i > 0 ? ", " : "") + m.Data[i].ToString();
            }

            Debug.Log("[OSCMaster | " + DateTime.Now.ToLocalTime() + "] " + m.Address + " : " + args);
        }

        var tempClient = new OSCClient(System.Net.IPAddress.Loopback, port);

        tempClient.SendTo(m, host, port);
        tempClient.Close();
    }
Exemple #4
0
    private void UpdateClientAddress()
    {
        IPAddress ipAddress;

        if (IPAddress.TryParse(ClientIP, out ipAddress) && 0 <= Port && Port <= 65535)
        {
            if (oscClient != null)
            {
                oscClient.Close();
            }
            oscClient = new OSCClient(ipAddress, Port);
            // Formatting might have changed
            ClientIP = oscClient.ClientIPAddress.ToString();
            Debug.Log($"OSC Client address set to {ClientIP}:{Port}.");
        }
        else
        {
            Debug.LogWarning($"Unable to set OSC client address to invalid IP/port: {ClientIP}:{Port}. OSC is still being sent to {oscClient.ClientIPAddress}:{oscClient.Port}.");
        }
        currentClientIP = ClientIP;
    }
 void OnApplicationQuit()
 {
     _OSCClient.Close();
 }
 private void OnDestroy()
 {
     client.Close();
 }
Exemple #7
0
 private void Close()
 {
     localhost.Close();
     client.Close();
 }
 void RemoveClient(OSCClient client)
 {
     client.Close();
     m_ClientsToRemove.Add(client);
 }
Exemple #9
0
    void ExecuteOSCCmd(string address, List <System.Object> dataList)
    {
        int count = dataList.Count;

        if (address.Equals("/play-video"))
        {
            if (count > 0)
            {
                ResetVideoRelatedParams();
                var url = dataList[0].ToString();
                if (!url.Contains("http"))
                {
                    if (url.Equals("-1"))
                    {
                        StopVideo();
                        DoNextCmd();
                        return;
                    }
                    var fileName = Path.GetFileName(url);
                    var fullPath = Path.Combine(Application.persistentDataPath, fileName);
                    url = "file://" + fullPath;
                    if (!File.Exists(fullPath))
                    {
                        StartCoroutine(ShowInfoForFixedDuration("no " + fileName, 3, 40));
                        return;
                    }
                }

                var isLooping = false;
                int val       = 0;
                if (count > 1)
                {
                    float.TryParse(dataList[1].ToString(), out firstPlayStartPos);
                }

                if (count > 2)
                {
                    float.TryParse(dataList[2].ToString(), out firstPlayStopPos);
                }

                if (count > 3 && int.TryParse(dataList[3].ToString(), out val))
                {
                    isLooping = val == 1;
                }

                if (count > 4)
                {
                    float.TryParse(dataList[4].ToString(), out loopingStartPos);
                }

                if (count > 5)
                {
                    float.TryParse(dataList[5].ToString(), out loopingStopPos);
                }

                PlayVideo(url, isLooping, false);
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/stop-video"))
        {
            StopVideo();
            DoNextCmd();
        }
        else if (address.Equals("/pause-video"))
        {
            if (curPlayer != null)
            {
                curPlayer.Pause();
            }
            DoNextCmd();
        }
        else if (address.Equals("/continue-video"))
        {
            if (curPlayer != null && !curPlayer.isPlaying)
            {
                ResetVideoRelatedParams();
                if (count > 0)
                {
                    float.TryParse(dataList[0].ToString(), out firstPlayStopPos);
                    if (firstPlayStopPos > 0)
                    {
                        stopFrame = (long)(curPlayer.frameCount * firstPlayStopPos);
                    }
                }

                if (count > 1)
                {
                    float.TryParse(dataList[1].ToString(), out loopingStartPos);
                }

                if (count > 2)
                {
                    float.TryParse(dataList[2].ToString(), out loopingStopPos);
                }
                curPlayer.Play();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/load-video"))
        {
            if (count > 0)
            {
                var url = dataList[0].ToString();
                if (!url.Contains("http"))
                {
                    if (url.Equals("-1"))
                    {
                        StopVideo();
                        return;
                    }
                    var fileName = Path.GetFileName(url);
                    var fullPath = Path.Combine(Application.persistentDataPath, fileName);
                    url = "file://" + fullPath;
                    if (!File.Exists(fullPath))
                    {
                        StartCoroutine(ShowInfoForFixedDuration("no " + fileName, 3, 40));
                        return;
                    }
                }

                LoadVideo(url, true, false, OnVideoLoaded);
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/download-file"))
        {
            if (count > 0)
            {
                var  url      = dataList[0].ToString();
                var  filename = Path.GetFileName(url);
                var  filePath = Path.Combine(Application.persistentDataPath, filename);
                int  val;
                bool testPlay  = false;
                bool overwrite = true;
                bool interruptDuringDownload = false;

                if (count > 1 && int.TryParse(dataList[1].ToString(), out val))
                {
                    interruptDuringDownload = val == 1;
                }

                if (count > 2 && int.TryParse(dataList[2].ToString(), out val))
                {
                    overwrite = val == 1;
                }

                if (count > 3 && int.TryParse(dataList[3].ToString(), out val))
                {
                    testPlay = val == 1;
                }

                if (!overwrite && File.Exists(filePath))
                {
                    return;
                }
                if (downloadRoutine != null)
                {
                    if (interruptDuringDownload)
                    {
                        StopCoroutine(downloadRoutine);
                    }
                    else
                    {
                        return;
                    }
                }
                downloadRoutine = StartCoroutine(DownloadFileToPath(url, filePath, () => {
                    var fileExist = File.Exists(filePath);
                    if (!fileExist)
                    {
                        StartCoroutine(ShowInfoForFixedDuration("download failed", 3, 40));
                        return;
                    }

                    if (testPlay)
                    {
                        PlayVideo("file://" + filePath, false, false);
                    }

                    if (oscClient != null)
                    {
                        OSCMessage message = new OSCMessage("/download-done");
                        message.Append(myIP);
                        message.Append(filename);
                        message.Append(fileExist? "succeed" : "failed");
                        oscClient.Send(message);
                    }

                    DoNextCmd();
                }));
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/set-server"))
        {
            if (count > 1)
            {
                if (oscClient != null)
                {
                    oscClient.Close();
                    oscClient = null;
                }

                targetServerIP = dataList[0].ToString();
                if (int.TryParse(dataList[1].ToString(), out port))
                {
                    oscClient = CreateOSCClient(targetServerIP, port);
                    var oscMsg = new OSCMessage("/set-server-response");
                    oscMsg.Append(myIP);
                    oscMsg.Append("connected");
                    oscClient.Send(oscMsg);
                }
                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/set-id"))
        {
            if (count > 0)
            {
                if (count > 1)
                {
                    int val = 1;
                    if (int.TryParse(dataList[1].ToString(), out val))
                    {
                        info.enabled = val == 1;
                    }
                }
                else
                {
                    info.enabled = !info.enabled;
                }

                if (info.enabled)
                {
                    info.fontSize = 90;
                    int index = -1;
                    int.TryParse(dataList[0].ToString(), out index);

                    if (index >= 0 && index < dancerNames.Length)
                    {
                        info.text = dancerNames[index];
                    }
                    else
                    {
                        info.text = dataList[0].ToString();
                    }
                }
                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/set-effect-param"))
        {
            if (count > 0)
            {
                int effectIndex = -1;
                if (int.TryParse(dataList[0].ToString(), out effectIndex))
                {
                    if (effectIndex >= 0 && effectIndex < effectList.Length)
                    {
                        bool toRecord = false;
                        int  temp     = 0;
                        if (count > 1)
                        {
                            int.TryParse(dataList[1].ToString(), out temp);
                            toRecord = temp == 1;

                            EffectParamsRecording.EffectParams paramFrame = null;
                            if (toRecord && paramRecording.state == EffectParamsRecording.State.Recording && curPlayer != null && curPlayer.isPlaying)
                            {
                                for (int i = 0; i < paramRecording.seq.Count; i++)
                                {
                                    var frameData = paramRecording.seq[i];
                                    if (frameData.effectIndex == effectIndex && frameData.videoFrame == curPlayer.frame)
                                    {
                                        paramFrame = frameData;
                                        break;
                                    }
                                }

                                if (paramFrame == null)
                                {
                                    paramFrame = new EffectParamsRecording.EffectParams();
                                    paramRecording.seq.Add(paramFrame);
                                    paramFrame.effectIndex = effectIndex;
                                    paramFrame.videoFrame  = curPlayer.frame;
                                }
                            }

                            var   effect     = effectList[effectIndex];
                            float val        = -1;
                            int   paramIndex = -1;

                            for (int i = 2; i < count;)
                            {
                                var data = dataList[i].ToString();
                                if (data.Equals("i"))
                                {
                                    if ((i + 2) < count && int.TryParse(dataList[i + 1].ToString(), out paramIndex) && float.TryParse(dataList[i + 2].ToString(), out val))
                                    {
                                        effect.SetParameter(paramIndex, val);
                                        if (paramFrame != null)
                                        {
                                            paramFrame.paramIndex.Add(paramIndex);
                                            paramFrame.paramVal.Add(val);
                                        }
                                    }
                                    i += 3;
                                }
                                else if (data.Equals("s"))
                                {
                                    int startIndex = 0;
                                    int paramCount = 0;
                                    if ((i + 2) < count && int.TryParse(dataList[i + 1].ToString(), out startIndex) && int.TryParse(dataList[i + 2].ToString(), out paramCount))
                                    {
                                        if (i + 2 + paramCount < count)
                                        {
                                            for (int k = 0; k < paramCount; k++)
                                            {
                                                if (float.TryParse(dataList[k + i + 3].ToString(), out val))
                                                {
                                                    paramIndex = startIndex + k;
                                                    effect.SetParameter(paramIndex, val);
                                                    if (paramFrame != null)
                                                    {
                                                        paramFrame.paramIndex.Add(paramIndex);
                                                        paramFrame.paramVal.Add(val);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    i += (3 + paramCount);
                                }
                                else
                                {
                                    i++;
                                }
                            }
                            effect.SetEffectActive(true);
                        }
                    }
                    else
                    {
                        StartCoroutine(ShowInfoForFixedDuration("effectIndex out of bound", 3, 25));
                    }
                    DoNextCmd();
                }
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/trigger-effect"))
        {
            if (count > 0)
            {
                int effectIndex = -1;
                if (int.TryParse(dataList[0].ToString(), out effectIndex))
                {
                    if (effectIndex >= 0 && effectIndex < effectList.Length)
                    {
                        paramRecording.Reset();
                        float duration = 1;
                        float.TryParse(dataList[1].ToString(), out duration);

                        var effect = effectList[effectIndex];
                        effect.ResetTriggerData();
                        effect.SetEffectRoutineDuration(duration);
                        List <object> funcParamBuf = new List <object>();
                        for (int i = 2; i < count;)
                        {
                            int paramIndex = -1, patternID = -1, funcParamCount = -1;
                            funcParamBuf.Clear();
                            int.TryParse(dataList[i].ToString(), out paramIndex);
                            int.TryParse(dataList[i + 1].ToString(), out patternID);
                            int.TryParse(dataList[i + 2].ToString(), out funcParamCount);
                            if (paramIndex >= 0 && patternID >= 0 && funcParamCount > 0)
                            {
                                for (int k = 0; k < funcParamCount; k++)
                                {
                                    funcParamBuf.Add(dataList[i + 3 + k].ToString());
                                }
                                var paramFunc = ParamPattern.GetFunc(
                                    (ParamPattern.PatternType)patternID,
                                    funcParamBuf);
                                effect.AddData(paramIndex, paramFunc);
                                i = i + 3 + funcParamCount;
                            }
                            else
                            {
                                i += 3;
                            }
                        }

                        effect.TriggerEffect(DoNextCmd);
                    }
                    else
                    {
                        StartCoroutine(ShowInfoForFixedDuration("effectIndex out of bound", 3, 25));
                        DoNextCmd();
                    }
                }
            }
            else
            {
                return;
            }
        }
        else if (address.Equals("/record-params"))
        {
            int policy = 0;
            if (count > 0)
            {
                int.TryParse(dataList[0].ToString(), out policy);
            }

            paramRecording.Reset();
            paramRecording.state = EffectParamsRecording.State.Recording;

            if (curPlayer != null && curPlayer.url != null)
            {
                switch (policy)
                {
                case 0:                         //replay
                    if (curPlayer.isPlaying)
                    {
                        PlayVideo(curPlayer.url, true, false);
                    }
                    break;

                case 1:                         //no replay
                    if (!curPlayer.isPlaying)
                    {
                        PlayVideo(curPlayer.url, true, false);
                    }
                    break;

                case 2:
                    //no action
                    break;
                }
            }
            DoNextCmd();
        }
        else if (address.Equals("/stop-recording"))
        {
            StopRecording();
            DoNextCmd();
        }
        else if (address.Equals("/clear-recording"))
        {
            paramRecording.Reset();
            DoNextCmd();
        }
        else if (address.Equals("/set-image"))
        {
            if (count > 0)
            {
                var imgName = dataList[0].ToString();
                if (imgName.Equals("-1"))
                {
                    stillImage.sprite = null;
                }
                else if (imgCache.ContainsKey(imgName))
                {
                    var img = imgCache[imgName];
                    stillImage.sprite = img;
                }
                else
                {
                    var sprite = LoadSprite(imgName);
                    if (sprite != null)
                    {
                        imgCache.Add(imgName, sprite);
                        stillImage.sprite = sprite;
                    }
                }

                if (count > 1)
                {
                    float alpha = 0;
                    if (float.TryParse(dataList[1].ToString(), out alpha))
                    {
                        var color = stillImage.color;
                        color.a            = alpha;
                        stillImage.color   = color;
                        stillImage.enabled = true;
                        bgCanvas.enabled   = true;
                    }
                }

                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/load-image"))
        {
            if (count > 0)
            {
                var imgName = dataList[0].ToString();
                var img     = LoadSprite(imgName);
                if (imgCache.ContainsKey(imgName))
                {
                    imgCache[imgName] = img;
                }
                else
                {
                    imgCache.Add(imgName, img);
                }

                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/disable-effect"))
        {
            if (count > 0)
            {
                if (dataList[0].ToString().Equals("-1"))
                {
                    DisableAllEffect();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        int effectIndex = -1;
                        if (int.TryParse(dataList[i].ToString(), out effectIndex))
                        {
                            if (effectList.Length > effectIndex && effectIndex >= 0)
                            {
                                effectList[effectIndex].SetEffectActive(false);
                            }
                        }
                    }
                }
            }
            paramRecording.Reset();
            DoNextCmd();
        }
        else if (address.Equals("/close-all"))
        {
            CloseAll();
            DoNextCmd();
        }
        else if (address.Equals("/show-text"))
        {
            if (count > 5)
            {
                TextManager.SentenceData sd = null;
                string content = dataList[0].ToString();

                if (content.Equals("-1"))
                {
                    sd = txtManager.GetRandSentenceData();
                }
                else
                {
                    sd      = new TextManager.SentenceData();
                    sd.main = content;
                }
                TextManager.Orientation orient = TextManager.Orientation.Horizontal;
                int   fontSize    = 28;
                float posX        = 0.5f;
                float posY        = 0.5f;
                int   effectIndex = 0;

                int val = 0;
                if (int.TryParse(dataList[1].ToString(), out val))
                {
                    orient = (TextManager.Orientation)val;
                }
                if (count > 2)
                {
                    int.TryParse(dataList[2].ToString(), out fontSize);
                }

                if (count > 3)
                {
                    float.TryParse(dataList[3].ToString(), out posX);
                }

                if (count > 4)
                {
                    float.TryParse(dataList[4].ToString(), out posY);
                }

                if (count > 5)
                {
                    int.TryParse(dataList[5].ToString(), out effectIndex);
                }

                dataBuf.Clear();
                for (int i = 6; i < count; i++)
                {
                    dataBuf.Add(dataList[i].ToString());
                }

                txtManager.ShowText(sd, orient, fontSize, posX, posY, effectIndex, dataBuf);
            }
            else
            {
                txtManager.ShowText();
            }

            DoNextCmd();
        }
        else if (address.Equals("/set-brightness"))
        {
            if (count > 0)
            {
                int val = 0;
                if (int.TryParse(dataList[0].ToString(), out val))
                {
                    DeviceUtils.SetScreenBrightness(val);
                }
            }

            DoNextCmd();
        }
        else if (address.Equals("/set-bg-color"))
        {
            if (count > 0)
            {
                float val   = 0;
                Color color = Color.white;
                int   i     = 0;
                foreach (var valStr in dataList)
                {
                    if (float.TryParse(valStr.ToString(), out val))
                    {
                        if (i == 0)
                        {
                            color.r = val;
                        }
                        else if (i == 1)
                        {
                            color.g = val;
                        }
                        else if (i == 2)
                        {
                            color.b = val;
                        }
                        else
                        {
                            color.a = val;
                        }
                    }
                    i++;
                }
                bg.color = color;
                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/set-video-alpha"))
        {
            if (count > 0)
            {
                float alpha = 1;
                if (float.TryParse(dataList[0].ToString(), out alpha))
                {
                    var color = videoImage.color;
                    color.a          = alpha;
                    videoImage.color = color;
                }

                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/set-video-speed"))
        {
            if (count > 0)
            {
                float factor = 0;
                if (float.TryParse(dataList[0].ToString(), out factor))
                {
                    SetVideoSpeed(factor);
                }

                DoNextCmd();
            }
        }
        else if (address.Equals("/set-start-pos"))
        {
            if (count > 0)
            {
                float val = -1;
                float.TryParse(dataList[0].ToString(), out val);
                SetLoopStartPos(val);
                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/get-battery-level"))
        {
            if (oscClient != null)
            {
                if (oscClient != null)
                {
                    OSCMessage message = new OSCMessage("/battery-level");
                    message.Append(SystemInfo.batteryLevel);
                    oscClient.Send(message);
                }
            }

            DoNextCmd();
        }
        else if (address.Equals("/set-text-effect-sentences-file"))
        {
            if (count > 0)
            {
                string path = GetTextFilePath(dataList[0].ToString());
                if (!File.Exists(path))
                {
                    StartCoroutine(ShowInfoForFixedDuration("no such text file", 3, 25));
                    return;
                }
                else
                {
                    PlayerPrefs.SetString(TextManager.sentenceInputPathKey, path);
                    txtManager.ProcessInputData(File.ReadAllText(path));
                }

                DoNextCmd();
            }
            else
            {
                DoNextCmd();
            }
        }
        else if (address.Equals("/reset-receiver"))
        {
            Debug.Log("reset receiver");
            reciever.Close();
            reciever = new OSCReciever();
            reciever.Open(oscPort);

            DoNextCmd();
        }
        //unused cmds
        else if (address.Equals("/set-canvas"))
        {
            if (count > 0)
            {
                int val = 1;
                if (int.TryParse(dataList[0].ToString(), out val))
                {
                    if (count > 1)
                    {
                        if (dataList[1].ToString().Equals("txt"))
                        {
                            txtCanvas.enabled = val == 1;
                        }
                        else
                        {
                            bgCanvas.enabled = val == 1;
                        }
                    }
                    else
                    {
                        bgCanvas.enabled = val == 1;
                    }
                }
            }
            else
            {
                return;
            }
        }
        else if (address.Equals("/set-camera"))
        {
            if (count > 0 && cam != null)
            {
                int val = 0;
                if (int.TryParse(dataList[0].ToString(), out val))
                {
                    cam.clearFlags = (CameraClearFlags)val;
                }
            }
        }
        else if (address.Equals("/cache-video"))
        {
            if (count > 0)
            {
                var url = dataList[0].ToString();
                CacheVideoPlayer(url);
            }
            else
            {
                return;
            }
        }
        else if (address.Equals("/release-video"))
        {
            if (count > 0)
            {
                var url = dataList[0].ToString();
                ReleaseVideoPlayer(url);
            }
            else
            {
                return;
            }
        }
    }