cleanupWebrtc method

dynamic cleanupWebrtc (
  1. int handleId,
  2. dynamic hangupRequest
)

Implementation

cleanupWebrtc(int handleId, hangupRequest) {
  Janus.log("Cleaning WebRTC stuff");
  Plugin pluginHandle = this.pluginHandles[handleId.toString()];
  if (pluginHandle == null) {
    // Nothing to clean
    return;
  }

  if (hangupRequest == true) {
    // Send a hangup request (we don't really care about the response)
    var request = {"janus": "hangup", "transaction": Janus.randomString(12)};
    if (pluginHandle.handleToken != null)
      request["token"] = pluginHandle.handleToken;
    if (this.apiSecret != null) request["apisecret"] = this.apiSecret;
    Janus.debug(
        "Sending hangup request (handle=" + handleId.toString() + "):");
    Janus.debug(request);
    if (this.websockets) {
      request["session_id"] = this.sessionId;
      request["handle_id"] = handleId;
      this.ws.send(jsonEncode(request));
    } else {
      GatewayCallbacks httpCallbacks = GatewayCallbacks();
      Janus.httpAPICall(
          this.server +
              "/" +
              this.sessionId.toString() +
              "/" +
              handleId.toString(),
          {
            'verb': 'POST',
            'withCredentials': this.withCredentials,
            'body': request,
          },
          httpCallbacks);
    }
  }
  // Cleanup stack
  pluginHandle.remoteStream = null;
  if (pluginHandle.volume != null) {
    if (pluginHandle.volume['local'] != null &&
        pluginHandle.volume['local']['timer'] != null)
      pluginHandle.volume['local']['timer'].cancel();
    if (pluginHandle.volume['remote'] != null &&
        pluginHandle.volume['remote']['timer'] != null)
      pluginHandle.volume['remote']['timer'].cancel();
  }
  pluginHandle.volume = {};
  if (pluginHandle.bitrate['timer'] != null)
    pluginHandle.bitrate['timer'].cancel();
  pluginHandle.bitrate['timer'] = null;
  pluginHandle.bitrate['bsnow'] = null;
  pluginHandle.bitrate['bsbefore'] = null;
  pluginHandle.bitrate['tsnow'] = null;
  pluginHandle.bitrate['tsbefore'] = null;
  pluginHandle.bitrate['value'] = null;
  try {
    // Try a MediaStreamTrack.stop() for each track
    if (pluginHandle.streamExternal && pluginHandle.myStream != null) {
      Janus.log("Stopping local stream tracks");
      // var tracks = pluginHandle.myStream.getTracks();
      // for (var mst in tracks) {
      //   Janus.log(mst);
      //   if (mst) mst.stop();
      // }
    }
  } catch (e) {
    // Do nothing if this fails
  }
  pluginHandle.streamExternal = false;
  pluginHandle.myStream = null;
  // Close PeerConnection
  try {
    pluginHandle.pc.close();
  } catch (e) {
    Janus.log(e.toString());
  }
  pluginHandle.pc = null;
  pluginHandle.candidates = [];
  pluginHandle.mySdp = null;
  pluginHandle.remoteSdp = null;
  pluginHandle.iceDone = false;
  pluginHandle.dataChannels = {};
  pluginHandle.dtmfSender = null;
  if (pluginHandle.onCleanup() is Function) pluginHandle.onCleanup();
}