2014-03-24 14:06:06 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 MOZILLA_AUDIOMIXER_H_
|
|
|
|
#define MOZILLA_AUDIOMIXER_H_
|
|
|
|
|
|
|
|
#include "AudioSampleFormat.h"
|
2019-10-31 13:01:53 +03:00
|
|
|
#include "AudioStream.h"
|
2014-03-24 14:06:06 +04:00
|
|
|
#include "nsTArray.h"
|
2014-08-25 16:13:08 +04:00
|
|
|
#include "mozilla/LinkedList.h"
|
2019-10-31 13:01:53 +03:00
|
|
|
#include "mozilla/NotNull.h"
|
|
|
|
#include "mozilla/PodOperations.h"
|
2014-03-24 14:06:06 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-08-25 16:13:08 +04:00
|
|
|
|
|
|
|
struct MixerCallbackReceiver {
|
|
|
|
virtual void MixerCallback(AudioDataValue* aMixedBuffer,
|
|
|
|
AudioSampleFormat aFormat, uint32_t aChannels,
|
|
|
|
uint32_t aFrames, uint32_t aSampleRate) = 0;
|
|
|
|
};
|
2014-03-24 14:06:06 +04:00
|
|
|
/**
|
|
|
|
* This class mixes multiple streams of audio together to output a single audio
|
|
|
|
* stream.
|
|
|
|
*
|
|
|
|
* AudioMixer::Mix is to be called repeatedly with buffers that have the same
|
2015-07-24 15:28:16 +03:00
|
|
|
* length, sample rate, sample format and channel count. This class works with
|
|
|
|
* interleaved and plannar buffers, but the buffer mixed must be of the same
|
|
|
|
* type during a mixing cycle.
|
2014-03-24 14:06:06 +04:00
|
|
|
*
|
|
|
|
* When all the tracks have been mixed, calling FinishMixing will call back with
|
|
|
|
* a buffer containing the mixed audio data.
|
|
|
|
*
|
|
|
|
* This class is not thread safe.
|
|
|
|
*/
|
|
|
|
class AudioMixer {
|
|
|
|
public:
|
2014-04-23 13:20:56 +04:00
|
|
|
AudioMixer() : mFrames(0), mChannels(0), mSampleRate(0) {}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-08-25 16:13:08 +04:00
|
|
|
~AudioMixer() {
|
2014-08-26 19:02:09 +04:00
|
|
|
MixerCallback* cb;
|
|
|
|
while ((cb = mCallbacks.popFirst())) {
|
|
|
|
delete cb;
|
|
|
|
}
|
2014-08-25 16:13:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void StartMixing() { mSampleRate = mChannels = mFrames = 0; }
|
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
/* Get the data from the mixer. This is supposed to be called when all the
|
|
|
|
* tracks have been mixed in. The caller should not hold onto the data. */
|
|
|
|
void FinishMixing() {
|
2014-08-25 16:13:08 +04:00
|
|
|
MOZ_ASSERT(mChannels && mFrames && mSampleRate,
|
|
|
|
"Mix not called for this cycle?");
|
|
|
|
for (MixerCallback* cb = mCallbacks.getFirst(); cb != nullptr;
|
|
|
|
cb = cb->getNext()) {
|
2018-04-26 09:30:31 +03:00
|
|
|
MixerCallbackReceiver* receiver = cb->mReceiver;
|
2019-10-31 13:01:53 +03:00
|
|
|
MOZ_ASSERT(receiver);
|
2018-04-26 09:30:31 +03:00
|
|
|
receiver->MixerCallback(mMixedAudio.Elements(),
|
|
|
|
AudioSampleTypeToFormat<AudioDataValue>::Format,
|
|
|
|
mChannels, mFrames, mSampleRate);
|
2014-08-25 16:13:08 +04:00
|
|
|
}
|
2014-03-24 14:06:06 +04:00
|
|
|
PodZero(mMixedAudio.Elements(), mMixedAudio.Length());
|
2014-04-23 13:20:56 +04:00
|
|
|
mSampleRate = mChannels = mFrames = 0;
|
2014-03-24 14:06:06 +04:00
|
|
|
}
|
|
|
|
|
2017-10-06 13:42:11 +03:00
|
|
|
/* Add a buffer to the mix. The buffer can be null if there's nothing to mix
|
|
|
|
* but the callback is still needed. */
|
2014-04-23 13:20:56 +04:00
|
|
|
void Mix(AudioDataValue* aSamples, uint32_t aChannels, uint32_t aFrames,
|
|
|
|
uint32_t aSampleRate) {
|
2014-03-24 14:06:06 +04:00
|
|
|
if (!mFrames && !mChannels) {
|
|
|
|
mFrames = aFrames;
|
|
|
|
mChannels = aChannels;
|
2014-04-23 13:20:56 +04:00
|
|
|
mSampleRate = aSampleRate;
|
2014-03-24 14:06:06 +04:00
|
|
|
EnsureCapacityAndSilence();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aFrames == mFrames);
|
|
|
|
MOZ_ASSERT(aChannels == mChannels);
|
2014-04-23 13:20:56 +04:00
|
|
|
MOZ_ASSERT(aSampleRate == mSampleRate);
|
2014-03-24 14:06:06 +04:00
|
|
|
|
2017-10-06 13:42:11 +03:00
|
|
|
if (!aSamples) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
for (uint32_t i = 0; i < aFrames * aChannels; i++) {
|
|
|
|
mMixedAudio[i] += aSamples[i];
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 16:13:08 +04:00
|
|
|
|
2019-10-31 13:01:53 +03:00
|
|
|
void AddCallback(NotNull<MixerCallbackReceiver*> aReceiver) {
|
2014-08-25 16:13:08 +04:00
|
|
|
mCallbacks.insertBack(new MixerCallback(aReceiver));
|
|
|
|
}
|
|
|
|
|
2014-08-25 17:25:49 +04:00
|
|
|
bool FindCallback(MixerCallbackReceiver* aReceiver) {
|
|
|
|
for (MixerCallback* cb = mCallbacks.getFirst(); cb != nullptr;
|
|
|
|
cb = cb->getNext()) {
|
|
|
|
if (cb->mReceiver == aReceiver) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-25 16:13:08 +04:00
|
|
|
bool RemoveCallback(MixerCallbackReceiver* aReceiver) {
|
|
|
|
for (MixerCallback* cb = mCallbacks.getFirst(); cb != nullptr;
|
|
|
|
cb = cb->getNext()) {
|
|
|
|
if (cb->mReceiver == aReceiver) {
|
|
|
|
cb->remove();
|
2014-08-26 19:02:09 +04:00
|
|
|
delete cb;
|
2014-08-25 16:13:08 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
private:
|
|
|
|
void EnsureCapacityAndSilence() {
|
|
|
|
if (mFrames * mChannels > mMixedAudio.Length()) {
|
|
|
|
mMixedAudio.SetLength(mFrames * mChannels);
|
|
|
|
}
|
|
|
|
PodZero(mMixedAudio.Elements(), mMixedAudio.Length());
|
|
|
|
}
|
|
|
|
|
2014-08-25 16:13:08 +04:00
|
|
|
class MixerCallback : public LinkedListElement<MixerCallback> {
|
|
|
|
public:
|
2019-10-31 13:01:53 +03:00
|
|
|
explicit MixerCallback(NotNull<MixerCallbackReceiver*> aReceiver)
|
2014-08-25 16:13:08 +04:00
|
|
|
: mReceiver(aReceiver) {}
|
2019-10-31 13:01:53 +03:00
|
|
|
NotNull<MixerCallbackReceiver*> mReceiver;
|
2014-08-25 16:13:08 +04:00
|
|
|
};
|
|
|
|
|
2014-03-24 14:06:06 +04:00
|
|
|
/* Function that is called when the mixing is done. */
|
2014-08-25 16:13:08 +04:00
|
|
|
LinkedList<MixerCallback> mCallbacks;
|
2014-03-24 14:06:06 +04:00
|
|
|
/* Number of frames for this mixing block. */
|
|
|
|
uint32_t mFrames;
|
|
|
|
/* Number of channels for this mixing block. */
|
|
|
|
uint32_t mChannels;
|
2014-04-23 13:20:56 +04:00
|
|
|
/* Sample rate the of the mixed data. */
|
|
|
|
uint32_t mSampleRate;
|
2014-03-24 14:06:06 +04:00
|
|
|
/* Buffer containing the mixed audio data. */
|
|
|
|
nsTArray<AudioDataValue> mMixedAudio;
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|
2014-03-24 14:06:06 +04:00
|
|
|
|
|
|
|
#endif // MOZILLA_AUDIOMIXER_H_
|