Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Sync video on host and clients

Discussion in 'Multiplayer' started by only18days, Oct 2, 2023.

  1. only18days

    only18days

    Joined:
    Jun 24, 2022
    Posts:
    3
    Hi all,
    I am trying to synchronize the video on both the host and clients.
    And I tried the method supplied by Bruno in this post. However, I've run into an issue where I'm unable to call the RpcPlayVideo function. I've attempted to troubleshoot this problem, but I haven't been able to find a solution yet. Any kind of help will be highly appreciated.

    Sincerely,
    Chloe

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using UnityEngine.Video;
    6. using UnityEngine.SceneManagement;
    7. public class SyncVideo : NetworkBehaviour
    8. {
    9.     public VideoPlayer videoPlayer;
    10.     [SerializeField]
    11.     private bool isPlaying = false;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.     }
    16.     void Awake()
    17.     {
    18.         SceneManager.sceneLoaded += OnSceneLoaded;
    19.     }
    20.     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    21.     {
    22.         videoPlayer = GameObject.Find("Plane").GetComponent<VideoPlayer>();
    23.     }
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (Input.GetKeyDown(KeyCode.P))
    28.         {
    29.             Debug.Log("Pressed the key P to play the video.");
    30.             if (isServer){
    31.                 RpcPlayVideo();
    32.             }
    33.          
    34.             else{
    35.                 CmdPlayVideo();
    36.                 Debug.Log("CmdPlayVideo");
    37.             }
    38.            
    39.         }
    40.     }
    41.     [ClientRpc]
    42.     void RpcPlayVideo()//server calls this method
    43.     {
    44.         Debug.Log("RpcPlayVideo");
    45.         StartPlaying();
    46.     }
    47.     [Command]
    48.     void CmdPlayVideo()//clients call this method
    49.     {
    50.         RpcPlayVideo();
    51.     }
    52.     void StartPlaying()
    53.     {
    54.         Debug.Log("Start playing");
    55.         //play or pause video locally
    56.         if (!isPlaying)
    57.         {
    58.             videoPlayer.Play();
    59.             isPlaying = true;
    60.         }
    61.     }
    62. }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    Please clarify what you mean by that. It sounds like you're physically unable to make a call, but we're talking code here, not phones. ;)

    Do you get an error message?
    Does it silently not do anything?
    What's the order of the logs you put in for server and client?
    What Networking framework are you using? There are so many out there ...

    Just to be clear: What you're trying here is to send a message for clients to start the video but it's not going to synchronize the playback across all clients due to network latency. Meaning the host will play the video instantly, client A may start playing it with +50 ms delay and client B on a high-ping connection starts it with +800 ms delay.

    If you want perfect synchronization, for example when multiple monitors can be viewed at the same time by viewers or where the video needs to be in synch with a centralized audio playback, you'd have to at least measure RTT to each client before starting the video, and then set up every client to play the video at a given time minus the average RTT. This can be done by playing the video +1 second later minus the RTT passed via the RPC. Or if the machines are connected to the Internet and they have synchronized their clocks, the playback would instruct the machines to play the video at a given timestamp like 12:23:34.
     
  3. only18days

    only18days

    Joined:
    Jun 24, 2022
    Posts:
    3
    @CodeSmile , thanks for your reply!
    Apologies for the vague explanation, I'm relatively new to game development. I employed the mirror network, and I didn't encounter any error messages, which is why I find this situation perplexing. The only information I received was a log entry stating "Pressed the key P to play the video" when I was acting as the host. As a result, I assumed that it silently failed to execute the RpcPlayVideo function :(