Mic input level in Firefox

368 views
Skip to first unread message

mail...@gmail.com

unread,
Mar 27, 2014, 8:10:19 AM3/27/14
to discuss...@googlegroups.com
I'm trying to implement an audio monitor that can flag potential mic issues on a call.

On Chrome using RTCPeerConnection.getStats() it's working nicely, and I can get the mic level from the 'audioInputLevel' value. I'm running into a brick wall trying to do the same thing on Firefox. I've tried getting the stats as follows:

//
// 'localStream' is stream obtained via gUM() ;
//
var track = localStream.getAudioTracks()[0] ;
options.pc.getStats(track,
    function(stats) {
        stats.forEach(function(entry) {
            console.log("Entry : id = " + entry.id + ", type = " + entry.type) ;
        }) ;
    },
    function(error) {...}) ;

but the report comes back empty.

Changing the selector in getStats() to 'null' gets a report back with the following entries:

Based on the IDL at http://mxr.mozilla.org/mozilla-release/source/dom/webidl/RTCStatsReport.webidl it looks like it should be supported (for this application RTCMediaStreamTrackStats.audioLevel seems to merit investigation), but I just can't get access to the MediaStreamTrack stats at all.

Are track statistics supported on FF yet? Am I doing something wrong? There seems to be precious little in the way of proper documentation/samples out there; this attachment to Bugzilla issue 950855 shows similar results to what I observed in my code.

I've tried FF Release (28), Aurora (29 - would have expected it to be 30), and Nightly (31) on Ubuntu, and Release (28) on Windows - all with same outcome.

If it's not supported, is there a roadmap as to when it will be? I've again drawn a blank on any info in that regard.

Any pointers much appreciated!

Philipp Hancke

unread,
Mar 27, 2014, 4:19:17 PM3/27/14
to discuss...@googlegroups.com
You could try using https://github.com/latentflip/hark -- it uses the web audio API and does not rely on getStats. It will only work with local media streams obtained via getUserMedia though.

Vikas

unread,
Mar 27, 2014, 5:19:08 PM3/27/14
to discuss...@googlegroups.com
Hi,

You can try post it here, as it looks similar thread. 

/Vikas

mail...@gmail.com

unread,
Mar 28, 2014, 6:11:24 AM3/28/14
to discuss...@googlegroups.com, philipp...@googlemail.com
That looks interesting, thanks Phillipp. His example page only seems to work in Chrome, but I'll play about with it to see what's implemented in Firefox.

mail...@gmail.com

unread,
Apr 9, 2014, 10:40:36 AM4/9/14
to discuss...@googlegroups.com, philipp...@googlemail.com
An update for anybody looking to do something similar, the Web Audio approach used by Hark that Philipp pointed me to works well on Chrome and Firefox. The one caveat is the organisation of the audio graphs. At the moment Firefox only accepts LocalMediaStream stream objects for RTCPeerConnection (see here), so the output of AnalyserNode can't be routed sequentially to RTCPeerConnection, but rather connect the source stream in parallel

Doesn't work:

 +-------+    +-------------+    +----+
 | gUM() |--->| AnalyseNode |--->| PC |
 +-------+    +-------------+    +----+

Works:

    +-------+    +----+
    | gUM() |--->| PC |
    +-------+    +----+
        |
        |
        V
 +-------------+
 | AnalyseNode |
 +-------------+

Code (outline):

  //
  // localStream will be stream from gUM()
  // pc will be created RTCPeerConnection
  // 
  // This example leaves out all the boilerplate for obtaining these
  //
  pc.addStream(localStream) ;
  var audioContext = new AudioContext() ;
  var analyser = audioContext.createAnalyser() ;
  var fftSize = 128 ; // Must be power of 2
  var fftBins = new Uint8Array(fftSize);
  analyser.fftSize = fftSize;
   var sourceNode = audioContext.createMediaStreamSource(localStream);

  //
  // Do something like this periodically
  //
  function monitorAudio() {
    analyser.getByteFrequencyData(fftBins);
    // Get average of spectrum
    // Can restrict this to a specific range of frequency bins 
    // depending on application (e.g. voice is typically 300-3000Hz)
    var total=0 ;
    for(var i=0;i<fftSize; ++i) {
      total+=fftBins[i] ;
    }
    var avg = total/fftSize ;
    if(avg==0) {...} // No audio
    if.... // Some other case
  }

Anyway, hope this helps somebody!

John

bryand...@gmail.com

unread,
Apr 9, 2014, 12:44:05 PM4/9/14
to discuss...@googlegroups.com
Nice.  Thanks for posting this!


--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages