2.1.3 • Published 4 years ago

freepbx-react-webrtc v2.1.3

Weekly downloads
10
License
MIT
Repository
github
Last release
4 years ago

MIT license PRs Welcome

This is a fork of the iotcomms implementation for general webrtc

To get started:

$ npm install freepbx-react-webrtc --save

The main objective of the component is to provide a high-level logic providing WebRTC functionality to web applications where the developer should not have to dig into the details of how SIP, Peer connections, Sessions and Video elements interact during the lifecycle of a call.

An example application is found here

Events that are called by component

EventDescription
IdleCalled when connected to service and there is no call coming in or going out
CallingCalled when there is an outgoing call
AlertingCalled when there is an incoming call going on
InCallCalled multiple times while call is connected and running
errorCalled when error is happening, passes 2 parameters (response, cause)

Events that are needed to get called to interact with the component

EventDescription
answerCallAccept an incoming call when there is one ("Alerting")
placeCallCalls the defined destination URI
hangupCallCancels outgoing or running Call
toggleMicrophoneToggles outgoing sound track (true / false). Used to mute outgoing sound only.
toggleVideoToggles outgoing video track (true / false). Used to mute outgoing video only.

To toggle incoming sound / video interact with the video element directly

Below is an example of how to embed the component in a React.js application

  import WebRTCClient from "freepbx-react-webrtc";
  import EventEmitter from "events";

  constructor(props) {
    super(props)

    this.state = {
        eventHandler: new EventEmitter()
    };
  }
  
  // Default Events that are called by the WebRTCClient
  componentDidMount() {
    // On Error
    this.state.eventHandler.on("error", function(response, cause) {
      // Do something...
    });

    // Connected and Idle
    this.state.eventHandler.on("Idle", function() {
      // Do something...
    });

    // Outgoing call happening
    this.state.eventHandler.on("Calling", function() {
      // Do something...
    });

    // Incoming Call
    this.state.eventHandler.on("Alerting", function() {
      // Do something...
    });

    // Call started
    this.state.eventHandler.on("InCall", function() {
      // Do something...
    });
  }
  
  acceptCall = () => {
  	this.state.eventHandler.emit("answerCall");
  }

  render() {
    return (
      <div className="App">
        //Video element used for rendering video self-view
        <video width="25%"  id="localVideo" autoPlay playsInline  muted="muted"></video>

        //Video element used for rendering video of remote party
        <video width="50%" id="remoteVideo" autoPlay playsInline ></video>


        // Example Accept Button
        <Button onClick={this.acceptCall}>Accept Call</Button>
		
        <WebRTCClient
          enableVideo={true}
          enableSound={true}
          webSocketPort="8089"
          autoRegister = {true}
          sipDomain={sipDomain}
          sipServer={sipServer}
          sipUser={sipUser}
          sipPassword={sipPassword}
          destination={destinationUri}
          metaData={metaDataObject}
          alertVideoUrl="alertUrl"
          ringbackVideoUrl="ringbackUrl"
          localVideoTagId="localVideo"
          remoteVideoTagId="remoteVideo"
          skipStunServer={false}
          stunServerList=[{urls: "stun:stun.l.google.com:19302"}]
          eventHandler={this.state.eventHandler}
          eventHandlerEmit={this.eventHandlerEmit}
          updateCallState={this.updateCallState}
          updateConnectionState={this.updateConnectionState}
          traceSip={true}
        />
      </div>
    );
  }
}

Component Parameters

ParameterDescription
enableVideoindicates if video should be enabled for calls
enableSoundindicates if sound should be enabled for calls
autoRegisterindicates if the component should send a SIP Register to be able to receive calls
sipDomainis the SIP domain to be used for registration and calls
sipServerThis is an optional property indicating where to connect with the server. If this is not set the value of sipDomain is used
sipUseris the user id to be used for authentication
destinationis the remote URI destination to be called
metaDatais an object to be passed to the remote side in a X-MetaData SIP header. The object is JSON stringified and then URL encoded before inserted as header value
alertVideoUrlis an optional sring with an URL pointing to a video file supported by the video element. This file is played when an inbound call is received. If the property is omitted a default file is played
ringbackVideoUrlis an optional sring with an URL pointing to a video file supported by the video element. This file is played when an call is placed until it has been answered. If the property is omitted a default file is played
skipStunServerindicates if a STUN server should be used. Disable when only working in local network
stunServerListlist of Dicts with STUN Servers to use
eventHandlerEventHandler to communicate between the parent and child component
eventHandlerEmitFunction to pass to the WebRTCClient so it can emit events
updateCallStateFunction that gets called with the CallState on change with a string as parameter
updateConnectionStateFunction that gets called with the ConnectionState with a string as parameter
traceSipBoolean to enable or disable advanced SIP output

The component will try to play out audio and video feedback when calling and alerting incoming calls. Due to autoplay limitations and logic in different web browsers this may not play.