2020-09-15 12:00:15 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef DOM_MEDIA_AUDIBILITYMONITOR_H_
|
|
|
|
#define DOM_MEDIA_AUDIBILITYMONITOR_H_
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "AudioSampleFormat.h"
|
|
|
|
#include "WebAudioUtils.h"
|
2020-09-23 14:45:44 +03:00
|
|
|
#include "AudioBlock.h"
|
2020-10-28 20:26:04 +03:00
|
|
|
#include "MediaData.h"
|
2020-09-15 12:00:15 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class AudibilityMonitor {
|
|
|
|
public:
|
|
|
|
// ≈ 20 * log10(pow(2, 12)), noise floor of 12bit audio
|
|
|
|
const float AUDIBILITY_THREHSOLD = -72.;
|
|
|
|
|
|
|
|
AudibilityMonitor(uint32_t aSamplerate, float aSilenceDurationSeconds)
|
|
|
|
: mSamplerate(aSamplerate),
|
|
|
|
mSilenceDurationSeconds(aSilenceDurationSeconds),
|
2020-09-15 12:01:42 +03:00
|
|
|
mSilentFramesInARow(0),
|
|
|
|
mEverAudible(false) {}
|
2020-09-15 12:00:15 +03:00
|
|
|
|
2020-09-23 14:45:44 +03:00
|
|
|
void Process(const AudioData* aData) {
|
2020-09-15 12:00:15 +03:00
|
|
|
ProcessInterleaved(aData->Data(), aData->mChannels);
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:45:44 +03:00
|
|
|
void Process(const AudioBlock& aData) {
|
|
|
|
if (aData.IsNull() || aData.IsMuted()) {
|
|
|
|
mSilentFramesInARow += aData.GetDuration();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ProcessPlanar(aData.ChannelData<float>(), aData.GetDuration());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProcessPlanar(const nsTArray<const float*>& aPlanar, TrackTime aFrames) {
|
|
|
|
uint32_t lastFrameAudibleAccrossChannels = 0;
|
|
|
|
for (uint32_t channel = 0; channel < aPlanar.Length(); channel++) {
|
|
|
|
uint32_t lastSampleAudible = 0;
|
|
|
|
for (uint32_t frame = 0; frame < aFrames; frame++) {
|
|
|
|
float dbfs = dom::WebAudioUtils::ConvertLinearToDecibels(
|
|
|
|
abs(aPlanar[channel][frame]), -100.f);
|
|
|
|
if (dbfs > AUDIBILITY_THREHSOLD) {
|
|
|
|
mEverAudible = true;
|
|
|
|
mSilentFramesInARow = 0;
|
|
|
|
lastSampleAudible = frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastFrameAudibleAccrossChannels =
|
|
|
|
std::max(lastFrameAudibleAccrossChannels, lastSampleAudible);
|
|
|
|
}
|
|
|
|
mSilentFramesInARow += aFrames - lastFrameAudibleAccrossChannels - 1;
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:00:15 +03:00
|
|
|
void ProcessInterleaved(const Span<AudioDataValue>& aInterleaved,
|
|
|
|
size_t aChannels) {
|
|
|
|
MOZ_ASSERT(aInterleaved.Length() % aChannels == 0);
|
|
|
|
uint32_t frameCount = aInterleaved.Length() / aChannels;
|
|
|
|
AudioDataValue* samples = aInterleaved.Elements();
|
|
|
|
|
|
|
|
uint32_t readIndex = 0;
|
|
|
|
for (uint32_t i = 0; i < frameCount; i++) {
|
2020-09-15 12:01:42 +03:00
|
|
|
bool atLeastOneAudible = false;
|
2020-09-15 12:00:15 +03:00
|
|
|
for (uint32_t j = 0; j < aChannels; j++) {
|
|
|
|
float dbfs = dom::WebAudioUtils::ConvertLinearToDecibels(
|
|
|
|
abs(AudioSampleToFloat(samples[readIndex++])), -100.f);
|
|
|
|
if (dbfs > AUDIBILITY_THREHSOLD) {
|
2020-09-15 12:01:42 +03:00
|
|
|
atLeastOneAudible = true;
|
2020-09-15 12:00:15 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 12:01:42 +03:00
|
|
|
if (atLeastOneAudible) {
|
2020-09-15 12:00:15 +03:00
|
|
|
mSilentFramesInARow = 0;
|
2020-09-15 12:01:42 +03:00
|
|
|
mEverAudible = true;
|
2020-09-15 12:00:15 +03:00
|
|
|
} else {
|
|
|
|
mSilentFramesInARow++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A stream is considered audible if there was audible content in the last
|
2020-09-15 12:01:42 +03:00
|
|
|
// `mSilenceDurationSeconds` seconds, or it has never been audible for now.
|
2020-09-15 12:00:15 +03:00
|
|
|
bool RecentlyAudible() {
|
2020-09-15 12:01:42 +03:00
|
|
|
return mEverAudible && (static_cast<float>(mSilentFramesInARow) /
|
|
|
|
mSamplerate) < mSilenceDurationSeconds;
|
2020-09-15 12:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const uint32_t mSamplerate;
|
|
|
|
const float mSilenceDurationSeconds;
|
|
|
|
uint64_t mSilentFramesInARow;
|
2020-09-15 12:01:42 +03:00
|
|
|
bool mEverAudible;
|
2020-09-15 12:00:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace mozilla
|
|
|
|
|
|
|
|
#endif // DOM_MEDIA_AUDIBILITYMONITOR_H_
|