getVolume method

dynamic getVolume (
  1. int handleId,
  2. dynamic remote
)

Implementation

getVolume(int handleId, remote) {
  Plugin pluginHandle = this.pluginHandles[handleId.toString()];
  if (pluginHandle == null) {
    Janus.warn("Invalid handle");
    return 0;
  }
  var stream = remote ? "remote" : "local";

  if (!pluginHandle.volume[stream])
    pluginHandle.volume[stream] = {'value': 0};
  // Start getting the volume, if audioLevel in getStats is supported (apparently
  // they're only available in Chrome/Safari right now: https://webrtc-stats.callstats.io/)
  if (pluginHandle.pc.getStats() != null &&
      (Janus.webRTCAdapter['browserDetails']['browser'] == "chrome" ||
          Janus.webRTCAdapter['browserDetails']['browser'] == "safari")) {
    if (remote && pluginHandle.remoteStream == null) {
      Janus.warn("Remote stream unavailable");
      return 0;
    } else if (remote == null && pluginHandle.myStream == null) {
      Janus.warn("Local stream unavailable");
      return 0;
    }
    if (pluginHandle.volume[stream]['timer'] == null) {
      Janus.log("Starting " + stream + " volume monitor");
      pluginHandle.volume[stream]['timer'] =
          Timer(Duration(microseconds: 200), () {
        pluginHandle.pc.getStats().then((List<StatsReport> stats) {
          stats.forEach((res) {
            if (res == null || res.type != "audio") return;
            if ((remote != null && !res.values['remoteSource']) ||
                (remote != null && res.type != "media-source")) return;
            pluginHandle.volume[stream]['value'] =
                (res.values['audioLevel'] ? res.values['audioLevel'] : 0);
          });
        });
      });
      return 0; // We don't have a volume to return yet
    }
    return pluginHandle.volume[stream]['value'];
  } else {
    // audioInputLevel and audioOutputLevel seem only available in Chrome? audioLevel
    // seems to be available on Chrome and Firefox, but they don't seem to work
    Janus.warn("Getting the " + stream + " volume unsupported by browser");
    return 0;
  }
}