Search code examples
javascriptwebrtcopentok

How to get mic activity for each stream in opentok


I have problem about how to get mic activity for each stream with OpenTok. I am working with PHP and OpenTok.js for this.

Here is my current code.

    ......
        var subscribers;

        session.on("streamCreated", function(event) {
           var subscriber = session.subscribe(event.stream, 'elementId', {
              insertMode: 'append',
              width: 100%,
              height: 100%
           })
           subscribers[subscriber.id] = subscriber;
           for(var user in subscribers) {
              console.log(subscribers[user].getAudioVolume());
           }
        });

..........

I tried like this, but always result is 100.

Who can solve this? Please help me. Yuri


Solution

  • var movingAvg = null;
    subscriber.on('audioLevelUpdated', function(event) {
      if (movingAvg === null || movingAvg <= event.audioLevel) {
    movingAvg = event.audioLevel;
      } else {
    movingAvg = 0.7 * movingAvg + 0.3 * event.audioLevel;
      }
    
      // 1.5 scaling to map the -30 - 0 dBm range to [0,1]
      var logLevel = (Math.log(movingAvg) / Math.LN10) / 1.5 + 1;
      logLevel = Math.min(Math.max(logLevel, 0), 1);
      document.getElementById('subscriberMeter').value = logLevel;
    });