Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / dom / media / AudioMixer.h
blob3db156a0ec025210e818b090e93a17acfbac21f8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZILLA_AUDIOMIXER_H_
7 #define MOZILLA_AUDIOMIXER_H_
9 #include "AudioSampleFormat.h"
10 #include "AudioStream.h"
11 #include "nsTArray.h"
12 #include "mozilla/LinkedList.h"
13 #include "mozilla/NotNull.h"
14 #include "mozilla/PodOperations.h"
16 namespace mozilla {
18 struct MixerCallbackReceiver {
19 virtual void MixerCallback(AudioDataValue* aMixedBuffer,
20 AudioSampleFormat aFormat, uint32_t aChannels,
21 uint32_t aFrames, uint32_t aSampleRate) = 0;
23 /**
24 * This class mixes multiple streams of audio together to output a single audio
25 * stream.
27 * AudioMixer::Mix is to be called repeatedly with buffers that have the same
28 * length, sample rate, sample format and channel count. This class works with
29 * interleaved and plannar buffers, but the buffer mixed must be of the same
30 * type during a mixing cycle.
32 * When all the tracks have been mixed, calling FinishMixing will call back with
33 * a buffer containing the mixed audio data.
35 * This class is not thread safe.
37 class AudioMixer {
38 public:
39 AudioMixer() : mFrames(0), mChannels(0), mSampleRate(0) {}
41 ~AudioMixer() {
42 MixerCallback* cb;
43 while ((cb = mCallbacks.popFirst())) {
44 delete cb;
48 void StartMixing() { mSampleRate = mChannels = mFrames = 0; }
50 /* Get the data from the mixer. This is supposed to be called when all the
51 * tracks have been mixed in. The caller should not hold onto the data. */
52 void FinishMixing() {
53 MOZ_ASSERT(mChannels && mSampleRate, "Mix not called for this cycle?");
54 for (MixerCallback* cb = mCallbacks.getFirst(); cb != nullptr;
55 cb = cb->getNext()) {
56 MixerCallbackReceiver* receiver = cb->mReceiver;
57 MOZ_ASSERT(receiver);
58 receiver->MixerCallback(mMixedAudio.Elements(),
59 AudioSampleTypeToFormat<AudioDataValue>::Format,
60 mChannels, mFrames, mSampleRate);
62 PodZero(mMixedAudio.Elements(), mMixedAudio.Length());
63 mSampleRate = mChannels = mFrames = 0;
66 /* Add a buffer to the mix. The buffer can be null if there's nothing to mix
67 * but the callback is still needed. */
68 void Mix(AudioDataValue* aSamples, uint32_t aChannels, uint32_t aFrames,
69 uint32_t aSampleRate) {
70 if (!mFrames && !mChannels) {
71 mFrames = aFrames;
72 mChannels = aChannels;
73 mSampleRate = aSampleRate;
74 EnsureCapacityAndSilence();
77 MOZ_ASSERT(aFrames == mFrames);
78 MOZ_ASSERT(aChannels == mChannels);
79 MOZ_ASSERT(aSampleRate == mSampleRate);
81 if (!aSamples) {
82 return;
85 for (uint32_t i = 0; i < aFrames * aChannels; i++) {
86 mMixedAudio[i] += aSamples[i];
90 void AddCallback(NotNull<MixerCallbackReceiver*> aReceiver) {
91 mCallbacks.insertBack(new MixerCallback(aReceiver));
94 bool FindCallback(MixerCallbackReceiver* aReceiver) {
95 for (MixerCallback* cb = mCallbacks.getFirst(); cb != nullptr;
96 cb = cb->getNext()) {
97 if (cb->mReceiver == aReceiver) {
98 return true;
101 return false;
104 bool RemoveCallback(MixerCallbackReceiver* aReceiver) {
105 for (MixerCallback* cb = mCallbacks.getFirst(); cb != nullptr;
106 cb = cb->getNext()) {
107 if (cb->mReceiver == aReceiver) {
108 cb->remove();
109 delete cb;
110 return true;
113 return false;
116 private:
117 void EnsureCapacityAndSilence() {
118 if (mFrames * mChannels > mMixedAudio.Length()) {
119 mMixedAudio.SetLength(mFrames * mChannels);
121 PodZero(mMixedAudio.Elements(), mMixedAudio.Length());
124 class MixerCallback : public LinkedListElement<MixerCallback> {
125 public:
126 explicit MixerCallback(NotNull<MixerCallbackReceiver*> aReceiver)
127 : mReceiver(aReceiver) {}
128 NotNull<MixerCallbackReceiver*> mReceiver;
131 /* Function that is called when the mixing is done. */
132 LinkedList<MixerCallback> mCallbacks;
133 /* Number of frames for this mixing block. */
134 uint32_t mFrames;
135 /* Number of channels for this mixing block. */
136 uint32_t mChannels;
137 /* Sample rate the of the mixed data. */
138 uint32_t mSampleRate;
139 /* Buffer containing the mixed audio data. */
140 nsTArray<AudioDataValue> mMixedAudio;
143 } // namespace mozilla
145 #endif // MOZILLA_AUDIOMIXER_H_