LoginSignup
0

More than 3 years have passed since last update.

WebRTCで何故かontracが呼ばれない場合の逃げ道

Posted at

RTCPeerConnection.connectionStateがconnectedになっていて、
きちんとMediaStreamが届いているはずなのになぜかontrackが呼ばれなかった時に取った回避策

connection.onconnectionstatechange = () => {
                if (connection.connectionState === 'connected') {
                    const tracks = connection.getReceivers().map(r => r.track);
                    const stream = new MediaStream(tracks);
                    //MediaStreamを使った処理
                }
            }

解説

  • connectionState === 'connected'は接続完了。普通はconnectionStateが一つ前の'connecting'の間にtrackイベントが発生してontrackに登録された処理が走る
  • Remoteから受け取ったMediaTrackは受け取ったRTCRtpReceiverに保持され、trackプロパティから取得できる
  • ontrack時にeventから取得できるMediaStreamはMediaTrackの配列を引数に手動で生成できる

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0