docs.unity3d.com
    目次を表示する/隠す

    チュートリアル

    WebRTC パッケージの基本的な利用方法について説明します。

    ネームスペースの追加

    ネームスペースは Unity.WebRTC を使用します。

    using UnityEngine;
    using Unity.WebRTC;
    

    ローカルピアの作成

    ローカルピアを作成し RTCDataChannel インスタンスを取得します。RTCDataChannel はバイナリデータの通信が可能です。OnOpen、OnClose にそれぞれコールバックを登録することで、RTCDataChannel の利用開始時と終了時に処理を実行できます。また、メッセージ受信時の処理を行うには OnMessage にコールバックを登録します。

        // ローカルピアの作成
            var localConnection = new RTCPeerConnection();
            var sendChannel = localConnection.CreateDataChannel("sendChannel");
            sendChannel.OnOpen = handleSendChannelStatusChange;
            sendChannel.OnClose = handleSendChannelStatusChange;
    

    リモートピアの作成

    リモートピアを作成して OnDataChannel にコールバックを設定します。

        // リモートピアの作成
        var remoteConnection = new RTCPeerConnection();
        remoteConnection.OnDataChannel = ReceiveChannelCallback;
    

    通信経路候補の登録

    ICE (Interactive Connectivity Establishment) の交換はピア間の接続確立に必要です。ピアの通信経路の候補が発見された時点で OnIceCandidate が呼び出されます。双方のピアが AddIceCandidate メソッドを呼び出して、通信経路候補の登録を行います。

    localConnection.OnIceCandidate = e => 
    { 
        !string.IsNullOrEmpty(e.candidate) || remoteConnection.AddIceCandidate(e); 
    }
    
    remoteConnection.OnIceCandidate = e => 
    { 
        !string.IsNullOrEmpty(e.candidate) || localConnection.AddIceCandidate(e);
    }
    

    シグナリング処理

    SDP の交換をピア間で行います。CreateOffer を呼び出して Offer SDP を生成します。Offer SDP を取得したら、ローカルピアとリモートピアの双方が SDP を設定します。SDP 交換の際に SetLocalDescription と SetRemoteDescription を混同して使用しないように注意してください。

    Offer SDP を設定したら, CreateAnswer を呼び出して Answer SDP を生成します。Offer SDP と同様に、ローカルピアとリモートピアの双方で Answer SDP を登録します。

    var op1 = localConnection.CreateOffer();
    yield return op1;
    var op2 = localConnection.SetLocalDescription(ref op1.desc);
    yield return op2;
    var op3 = remoteConnection.SetRemoteDescription(ref op1.desc);
    yield return op3;
    var op4 = remoteConnection.CreateAnswer();
    yield return op4;
    var op5 = remoteConnection.setLocalDescription(op4.desc);
    yield return op5;
    var op6 = localConnection.setRemoteDescription(op4.desc);
    yield return op6;
    

    ICE コネクションの状態の監視

    SDP 交換の際に ICE の交換が行われます。ICE コネクションの状態を確認するには、 OnIceConnectionChange コールバックを利用します。

    localConnection.OnIceConnectionChange = state => {
        Debug.Log(state);
    }
    

    DataChannel の接続

    ICE の交換が終了すると OnDataChannel が呼び出され RTCDataChannel インスタンスが生成されます。 OnMessage にコールバックを設定して、メッセージ受信時の処理を記述します。

    RTCDataChannel receiveChannel;
    void ReceiveChannelCallback(RTCDataChannel channel) 
    {
        receiveChannel = channel;
        receiveChannel.OnMessage = HandleReceiveMessage;  
    }
    

    メッセージの送信

    ピア双方の RTCDataChannel がオープンしたら、メッセージの通信が可能になります。string や byte[] などの型のデータを送信できます。

    void SendMessage(string message)
    {
        sendChannel.Send(message);
    }
    
    void SendBinary(byte[] bytes)
    {
        sendChannel.Send(bytes);
    }
    

    メッセージの受信

    メッセージを受信したとき、OnMessage に登録したコールバックが実行されます。byte[] 型のメッセージとして受け取るので、テキストとして扱う際には以下のように処理します。

    void HandleReceiveMessage(byte[] bytes)
    {
        var message = System.Text.Encoding.UTF8.GetString(bytes);
        Debug.Log(message);
    }
    

    終了処理

    終了する際には RTCDataChannel と RTCPeerConnection の Close メソッドをそれぞれ呼び出す必要があります。

    private void OnDestroy()
    {
      sendChannel.Close();
      receiveChannel.Close();
    
      localConnection.Close();
      remoteConnection.Close();
    }
    

    次のステップ

    このパッケージはビデオ/オーディオのストリーミングに関するサンプルを提供しています。このページ を参考に試してみてください。

    トップに戻る
    Copyright © 2023 Unity Technologies
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on Wednesday, November 1, 2023