feross / simple-peer

📡 Simple WebRTC video, voice, and data channels
MIT License
7.43k stars 974 forks source link

Uncaught RTCError: Transport channel closed and app.js:73056 Uncaught Error: Connection failed. at makeError (app.js:73056) at Peer._onConnectionStateChange (app.js:73698) at RTCPeerConnection.Peer._pc.onconnectionstatechange (app.js:73160) #705

Closed jeevatrippy closed 4 years ago

jeevatrippy commented 4 years ago

i create one video call application Laravel & ReactJS (based on WebRTC) , in this when i call to another member i get this error

app.js:8443 Uncaught RTCError: Transport channel closed

app.js:73056 Uncaught Error: Connection failed. at makeError (app.js:73056) at Peer._onConnectionStateChange (app.js:73698) at RTCPeerConnection.Peer._pc.onconnectionstatechange (app.js:73160)

here is my code

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import MediaHandler from '../MediaHandler'
import Pusher from "pusher-js";
import Peer from 'simple-peer';

const APP_KEY='347xxxxxxxxxaf756';

export default class App extends Component{

    constructor() {
        super();

        this.state = {
            hasMedia: false,
            otherUserId: null
        };

        this.user = window.user;
        this.user.stream = null;
        this.peers = {};

        this.mediaHandler = new MediaHandler();
        this.setupPusher();

        this.callTo = this.callTo.bind(this);
        this.setupPusher = this.setupPusher.bind(this);
        this.startPeer = this.startPeer.bind(this);
    }

    UNSAFE_componentWillMount(){
        this.mediaHandler.getPermissions()
            .then((stream) => {
                this.setState({hasMedia: true});
                this.user.stream = stream;

                try {
                    this.myVideo.srcObject = stream;
                } catch (e) {
                    this.myVideo.src = URL.createObjectURL(stream);
                }

                this.myVideo.play();
            })
    }

    setupPusher() {
        this.pusher = new Pusher(APP_KEY, {
            authEndpoint: '/pusher/auth',
            cluster: 'ap2',
            auth: {
                params: this.user.id,
                headers: {
                    'X-CSRF-Token': window.csrfToken
                }
            }
        });

        this.channel = this.pusher.subscribe('presence-video-channel');

        this.channel.bind(`client-signal-${this.user.id}`, (signal) => {
            let peer = this.peers[signal.userId];

            // if peer is not already exists, we got an incoming call
            if(peer === undefined) {
                this.setState({otherUserId: signal.userId});
                peer = this.startPeer(signal.userId, false);
            }

            peer.signal(signal.data);
        });
    }

    startPeer(userId, initiator = true) {
        const peer = new Peer({
            initiator,
            stream: this.user.stream,
            trickle: false
        });

        peer.on('signal', (data) => {
            this.channel.trigger(`client-signal-${userId}`, {
                type: 'signal',
                userId: this.user.id,
                data: data
            });
        });

        peer.on('stream', (stream) => {
            try {
                this.userVideo.srcObject = stream;
            } catch (e) {
                this.userVideo.src = URL.createObjectURL(stream);
            }

            // this.userVideo.pause();
            var playPromise =this.userVideo.play();
            if (playPromise !== undefined) {
                playPromise.then(_ => {
                    // Automatic playback started!
                    // Show playing UI.
                })
                    .catch(error => {
                        // Auto-play was prevented
                        // Show paused UI.
                    });
            }
        });

        peer.on('close', () => {
            let peer = this.peers[userId];
            if(peer !== undefined) {
                peer.destroy();
            }

            this.peers[userId] = undefined;
        });

        return peer;
    }

    callTo(userId) {
        this.peers[userId] = this.startPeer(userId);
    }

    render(){

        return(
            <div className="App">
                {[1,2,3,4].map((userId) => {
                    return this.user.id !== userId ? <button key={userId} onClick={() => this.callTo(userId)}>Call {userId}</button> : null;
                })}
                <div className="video-container">
                    <video className="my-video" ref={(ref) => {this.myVideo = ref;}}></video>
                    <video className="user-video" ref={(ref) => {this.userVideo = ref;}}></video>
                </div>
             </div>
        );
    }
}

if (document.getElementById('app')) {
    ReactDOM.render(<App />, document.getElementById('app'));
}

anyone help mee thanks in advance

RahulDey12 commented 4 years ago

I am also facing this same issue

nazar-pc commented 4 years ago

There are many reasons this could happen. I'm not going to debug your code, sorry. If you can make reproducible demo or clearly define where is the issue in simple-peer itself that is fine, otherwise this is not a generic WebRTC support forum, so we can't physically help everyone with their apps.

kk-007 commented 4 years ago

I'm also facing the same issue when i run code on broadband or wifi connection. when my both peer device is on cellular data then it works well try it on cellular data.

kk-007 commented 4 years ago

issue solved by doing these changes to bundle created by browserify or watchify kk-007/webrtc-video-call@62fd277

t-mullen commented 4 years ago

Duplicate of #657