DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (fddffdeab170)

VCS Links

Line Code
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/* -*- 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_SCRATCHBUFFER_H_
#define MOZILLA_SCRATCHBUFFER_H_
#include <mozilla/PodOperations.h>
#include <algorithm>

namespace mozilla {

/**
 * The classes in this file provide a interface that uses frames as a unit.
 * However, they store their offsets in samples (because it's handy for pointer
 * operations). Those functions can convert between the two units.
 */
static inline uint32_t FramesToSamples(uint32_t aChannels, uint32_t aFrames) {
  return aFrames * aChannels;
}

static inline uint32_t SamplesToFrames(uint32_t aChannels, uint32_t aSamples) {
  MOZ_ASSERT(!(aSamples % aChannels), "Frame alignment is wrong.");
  return aSamples / aChannels;
}

/**
 * Class that gets a buffer pointer from an audio callback and provides a safe
 * interface to manipulate this buffer, and to ensure we are not missing frames
 * by the end of the callback.
 */
template<typename T, uint32_t CHANNELS>
class AudioCallbackBufferWrapper
{
public:
  AudioCallbackBufferWrapper()
    : mBuffer(nullptr),
      mSamples(0),
      mSampleWriteOffset(1)
  {}
  /**
   * Set the buffer in this wrapper. This is to be called at the beginning of
   * the callback.
   */
  void SetBuffer(T* aBuffer, uint32_t aFrames) {
    MOZ_ASSERT(!mBuffer && !mSamples,
        "SetBuffer called twice.");
    mBuffer = aBuffer;
    mSamples = FramesToSamples(CHANNELS, aFrames);
    mSampleWriteOffset = 0;
  }

  /**
   * Write some frames to the internal buffer. Free space in the buffer should
   * be check prior to calling this.
   */
  void WriteFrames(T* aBuffer, uint32_t aFrames) {
    MOZ_ASSERT(aFrames <= Available(),
        "Writing more that we can in the audio buffer.");

    PodCopy(mBuffer + mSampleWriteOffset, aBuffer, FramesToSamples(CHANNELS,
                                                                   aFrames));
    mSampleWriteOffset += FramesToSamples(CHANNELS, aFrames);
  }

  /**
   * Number of frames that can be written to the buffer.
   */
  uint32_t Available() {
    return SamplesToFrames(CHANNELS, mSamples - mSampleWriteOffset);
  }

  /**
   * Check that the buffer is completly filled, and reset internal state so this
   * instance can be reused.
   */
  void BufferFilled() {
    // It's okay to have exactly zero samples here, it can happen we have an
    // audio callback driver because of a hint on MSG creation, but the
    // AudioOutputStream has not been created yet, or if all the streams have finished
    // but we're still running.
    // Note: it's also ok if we had data in the scratch buffer - and we usually do - and
    // all the streams were ended (no mixer callback occured).
    // XXX Remove this warning, or find a way to avoid it if the mixer callback
    // isn't called.
    NS_WARN_IF_FALSE(Available() == 0 || mSampleWriteOffset == 0,
            "Audio Buffer is not full by the end of the callback.");
    // Make sure the data returned is always set and not random!
    if (Available()) {
      PodZero(mBuffer + mSampleWriteOffset, FramesToSamples(CHANNELS, Available()));
    }
    MOZ_ASSERT(mSamples, "Buffer not set.");
    mSamples = 0;
    mSampleWriteOffset = 0;
    mBuffer = nullptr;
  }

private:
  /* This is not an owned pointer, but the pointer passed to use via the audio
   * callback. */
  T* mBuffer;
  /* The number of samples of this audio buffer. */
  uint32_t mSamples;
  /* The position at which new samples should be written. We want to return to
   * the audio callback iff this is equal to mSamples. */
  uint32_t mSampleWriteOffset;
};

/**
 * This is a class that interfaces with the AudioCallbackBufferWrapper, and is
 * responsible for storing the excess of data produced by the MediaStreamGraph
 * because of different rounding constraints, to be used the next time the audio
 * backend calls back.
 */
template<typename T, uint32_t BLOCK_SIZE, uint32_t CHANNELS>
class SpillBuffer
{
public:
  SpillBuffer()
  : mPosition(0)
  {
    PodArrayZero(mBuffer);
  }
  /* Empty the spill buffer into the buffer of the audio callback. This returns
   * the number of frames written. */
  uint32_t Empty(AudioCallbackBufferWrapper<T, CHANNELS>& aBuffer) {
    uint32_t framesToWrite = std::min(aBuffer.Available(),
                                      SamplesToFrames(CHANNELS, mPosition));

    aBuffer.WriteFrames(mBuffer, framesToWrite);

    mPosition -= FramesToSamples(CHANNELS, framesToWrite);
    // If we didn't empty the spill buffer for some reason, shift the remaining data down
    if (mPosition > 0) {
      PodMove(mBuffer, mBuffer + FramesToSamples(CHANNELS, framesToWrite),
              mPosition);
    }

    return framesToWrite;
  }
  /* Fill the spill buffer from aInput, containing aFrames frames, return the
   * number of frames written to the spill buffer */
  uint32_t Fill(T* aInput, uint32_t aFrames) {
    uint32_t framesToWrite = std::min(aFrames,
                                      BLOCK_SIZE - SamplesToFrames(CHANNELS,
                                                                   mPosition));

    PodCopy(mBuffer + mPosition, aInput, FramesToSamples(CHANNELS,
                                                         framesToWrite));

    mPosition += FramesToSamples(CHANNELS, framesToWrite);

    return framesToWrite;
  }
private:
  /* The spilled data. */
  T mBuffer[BLOCK_SIZE * CHANNELS];
  /* The current write position, in samples, in the buffer when filling, or the
   * amount of buffer filled when emptying. */
  uint32_t mPosition;
};

} // namespace mozilla

#endif // MOZILLA_SCRATCHBUFFER_H_