summaryrefslogtreecommitdiffstats
path: root/dom/media/AudioMixer.h
blob: bd8c02a8282e8c8e5fc450bb1fc4483688c9442a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -*- 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"
#include "AudioSegment.h"
#include "AudioStream.h"
#include "nsTArray.h"
#include "mozilla/NotNull.h"
#include "mozilla/PodOperations.h"

namespace mozilla {

struct MixerCallbackReceiver {
  // MixerCallback MAY modify aMixedBuffer but MUST clear
  // aMixedBuffer->mBuffer if its data is to live longer than the duration of
  // the callback.
  virtual void MixerCallback(AudioChunk* aMixedBuffer,
                             uint32_t aSampleRate) = 0;
};
/**
 * 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
 * length, sample rate, sample format and channel count. This class works with
 * planar buffers.
 *
 * When all the tracks have been mixed, calling MixedChunk() will provide
 * a buffer containing the mixed audio data.
 *
 * This class is not thread safe.
 */
class AudioMixer {
 public:
  AudioMixer() { mChunk.mBufferFormat = AUDIO_OUTPUT_FORMAT; }

  ~AudioMixer() = default;

  void StartMixing() {
    mChunk.mDuration = 0;
    mSampleRate = 0;
  }

  /* Get the data from the mixer. This is supposed to be called when all the
   * tracks have been mixed in. The caller MAY modify the chunk but MUST clear
   * mBuffer if its data needs to survive the next call to Mix(). */
  AudioChunk* MixedChunk() {
    MOZ_ASSERT(mSampleRate, "Mix not called for this cycle?");
    mSampleRate = 0;
    return &mChunk;
  };

  /* Add a buffer to the mix. The buffer can be null if there's nothing to mix
   * but the callback is still needed. */
  void Mix(AudioDataValue* aSamples, uint32_t aChannels, uint32_t aFrames,
           uint32_t aSampleRate) {
    if (!mChunk.mDuration) {
      mChunk.mDuration = aFrames;
      MOZ_ASSERT(aChannels > 0);
      mChunk.mChannelData.SetLength(aChannels);
      mSampleRate = aSampleRate;
      EnsureCapacityAndSilence();
    }

    MOZ_ASSERT(aFrames == mChunk.mDuration);
    MOZ_ASSERT(aChannels == mChunk.ChannelCount());
    MOZ_ASSERT(aSampleRate == mSampleRate);

    if (!aSamples) {
      return;
    }

    for (uint32_t i = 0; i < aFrames * aChannels; i++) {
      mChunk.ChannelDataForWrite<AudioDataValue>(0)[i] += aSamples[i];
    }
  }

 private:
  void EnsureCapacityAndSilence() {
    uint32_t sampleCount = mChunk.mDuration * mChunk.ChannelCount();
    if (!mChunk.mBuffer || sampleCount > mSampleCapacity) {
      CheckedInt<size_t> bufferSize(sizeof(AudioDataValue));
      bufferSize *= sampleCount;
      mChunk.mBuffer = SharedBuffer::Create(bufferSize);
      mSampleCapacity = sampleCount;
    }
    MOZ_ASSERT(!mChunk.mBuffer->IsShared());
    mChunk.mChannelData[0] =
        static_cast<SharedBuffer*>(mChunk.mBuffer.get())->Data();
    for (size_t i = 1; i < mChunk.ChannelCount(); ++i) {
      mChunk.mChannelData[i] =
          mChunk.ChannelData<AudioDataValue>()[0] + i * mChunk.mDuration;
    }
    PodZero(mChunk.ChannelDataForWrite<AudioDataValue>(0), sampleCount);
  }

  /* Buffer containing the mixed audio data. */
  AudioChunk mChunk;
  /* Size allocated for mChunk.mBuffer. */
  uint32_t mSampleCapacity = 0;
  /* Sample rate the of the mixed data. */
  uint32_t mSampleRate = 0;
};

}  // namespace mozilla

#endif  // MOZILLA_AUDIOMIXER_H_