コード例 #1
0
 public static void InitializeSingleton(string ConfigFile, int TickDelay, int TicksTillConverge, VideoPlayer VPlayer, string ServerIPAddress)
 {
     if (instance == null)
     {
         VideoSynchroniser.instance = new VideoSynchroniser(ConfigFile, TickDelay, TicksTillConverge, VPlayer, ServerIPAddress);
     }
 }
コード例 #2
0
    public void SendAdjustFrame()
    {
        IsAdjusting = true;
        foreach (IPAddress address in ActiveAddresses)
        {
            Debug.Log("Adjusting with IP " + address.ToString());
            if (Received.ContainsKey(address))
            {
                continue;
            }
            TcpClient     client        = new TcpClient(address.ToString(), Port);
            IPAddress     clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
            NetworkStream stream        = client.GetStream();

            String data  = string.Format("{0}|{1}", (int)CMD.AdjustFrame, VideoSynchroniser.Instance().CurrentTick);
            byte[] bytes = GenerateByteMessage(data);
            stream.Write(bytes, 0, bytes.Length);
            data = GetMessageFromNetworkStream(stream);
            Received.Add(clientAddress, int.Parse(data));
            AdjustedCnt++;
        }
        Debug.Log("AdjustCnt " + AdjustedCnt + " AllowedCnt " + ActiveAddresses.Count);
        Monitor.Enter(LockObj);
        Int32 minFrame = Int32.MaxValue;

        if (AdjustedCnt == ActiveAddresses.Count)
        {
            Debug.Log("Adjsting!!!");
            foreach (KeyValuePair <IPAddress, Int32> item in Sent)
            {
                minFrame = Math.Min(minFrame, item.Value);
            }
            foreach (KeyValuePair <IPAddress, Int32> item in Received)
            {
                minFrame = Math.Min(minFrame, item.Value);
            }
            if (minFrame != Int32.MaxValue)
            {
                VideoSynchroniser.Instance().AdjustedFrame = minFrame;
            }
        }
        Sent.Clear();
        Received.Clear();
        AdjustedCnt = 0;
        MyFrame     = -1;
        IsAdjusting = false;
        ActiveAddresses.Clear();
        foreach (IPAddress el in SecondaryActiveAddresses)
        {
            ActiveAddresses.Add(el);
        }
    }
コード例 #3
0
 private void Update()
 {
     AccumulatedTime += Time.deltaTime;
     if (AccumulatedTime > TickDelay)
     {
         AccumulatedTime = 0;
         int receivedFrame = VideoSynchroniser.Instance().IncreaseCurrentTick((int)VPlayer.frame);
         if (receivedFrame != -1)
         {
             VPlayer.frame = receivedFrame;
         }
     }
 }
コード例 #4
0
    private static void ReceiveHelloMessage(IAsyncResult res)
    {
        KeyValuePair <TCPServer, TcpClient> pair = (KeyValuePair <TCPServer, TcpClient>)res.AsyncState;
        TCPServer serv   = pair.Key;
        TcpClient client = pair.Value;

        IPAddress     address = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
        NetworkStream stream  = client.GetStream();

        Debug.Log("Received Hello from " + address.ToString());

        byte[] bytes = GenerateByteMessage("");
        stream.Write(bytes, 0, bytes.Length);

        string data = GetMessageFromNetworkStream(stream);

        string[] chunks = data.Split('|');
        VideoSynchroniser.Instance().CurrentTick = int.Parse(chunks[1]);

        client.Close();
    }
コード例 #5
0
    void Start()
    {
        // Will attach a VideoPlayer to the main camera.
        GameObject camera = GameObject.Find("Main Camera");

        // VideoPlayer automatically targets the camera backplane when it is added
        // to a camera object, no need to change videoPlayer.targetCamera.
        VPlayer = camera.AddComponent <UnityEngine.Video.VideoPlayer>();

        // Play on awake defaults to true. Set it to false to avoid the url set
        // below to auto-start playback since we're in Start().
        VPlayer.playOnAwake = false;

        // By default, VideoPlayers added to a camera will use the far plane.
        // Let's target the near plane instead.
        VPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;

        // This will cause our Scene to be visible through the video being played.
        VPlayer.targetCameraAlpha = 0.5F;

        // Set the video to play. URL supports local absolute or relative paths.
        // Here, using absolute.
        VPlayer.url = videoFile;

        // Restart from beginning when done.
        VPlayer.isLooping = true;

        // Each time we reach the end, we slow down the playback by a factor of 10.
        VPlayer.loopPointReached += EndReached;

        // Start playback. This means the VideoPlayer may have to prepare (reserve
        // resources, pre-load a few frames, etc.). To better control the delays
        // associated with this preparation one can use videoPlayer.Prepare() along with
        // its prepareCompleted event.
        VPlayer.Play();

        VideoSynchroniser.InitializeSingleton(ConfigFile, TickDelay, TicksTillConverge, VPlayer, ServerIPAddress);
    }
コード例 #6
0
    public void SendKeepAlive()
    {
        try
        {
            Monitor.PulseAll(LockObj);
            Monitor.Exit(LockObj);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        foreach (IPAddress address in ActiveAddresses)
        {
            Debug.Log("Sending Keepalive to IP " + address.ToString());
            TcpClient     client = new TcpClient(address.ToString(), Port);
            NetworkStream stream = client.GetStream();
            String        data   = string.Format("{0}|{1}", (int)CMD.KeepAlive, VideoSynchroniser.Instance().CurrentTick);
            byte[]        bytes  = GenerateByteMessage(data);
            stream.Write(bytes, 0, bytes.Length);

            client.Close();
        }
    }
コード例 #7
0
    private void Listen()
    {
        this.server = new TcpListener(Address, Port);
        this.server.Start();
        Debug.Log("Server Started");
        while (true)
        {
            Debug.Log("Waiting for connection");
            TcpClient client = server.AcceptTcpClient();
            Debug.Log("Connected!");
            IPAddress clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
            if (!AllowedAddresses.Contains(clientAddress))
            {
                continue;
            }
            NetworkStream stream = client.GetStream();

            String data = GetMessageFromNetworkStream(stream);
            Debug.Log(data);
            CMD cmd = (CMD)(data[0] - '0');
            Debug.Log("dddddddddd " + cmd);
            switch (cmd)
            {
            case CMD.Hello:
                Debug.Log("Received Hello message from " + clientAddress.ToString());
                data = string.Format("{0}|{1}", (int)CMD.Hello, VideoSynchroniser.Instance().CurrentTick);
                byte[] bytes = GenerateByteMessage(data);
                stream.Write(bytes, 0, bytes.Length);

                if (IsAdjusting)
                {
                    SecondaryActiveAddresses.Add(clientAddress);
                }
                else
                {
                    ActiveAddresses.Add(clientAddress);
                }
                break;

            case CMD.KeepAlive:
                Debug.Log("Received KeepAlive message from " + clientAddress.ToString());
                if (IsAdjusting)
                {
                    if (!SecondaryActiveAddresses.Contains(clientAddress))
                    {
                        Debug.Log("Adding in SecondaryActiveAddresses");
                        SecondaryActiveAddresses.Add(clientAddress);
                    }
                }
                else
                {
                    if (!ActiveAddresses.Contains(clientAddress))
                    {
                        Debug.Log("Adding in ActiveAddresses");
                        ActiveAddresses.Add(clientAddress);
                    }
                }
                break;

            case CMD.AdjustFrame:
                Debug.Log("Received AdjustFrame message from: " + clientAddress.ToString());

                string[] chunks = data.Split('|');
                Received.Add(clientAddress, int.Parse(chunks[1]));
                while (MyFrame == -1)
                {
                    continue;
                }
                data  = MyFrame.ToString();
                bytes = System.Text.Encoding.ASCII.GetBytes(data);
                stream.Write(bytes, 0, bytes.Length);
                client.Close();
                AdjustedCnt++;
                break;
            }
            client.Close();
        }
    }