2012-04-30 03:11:19 +00: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_AUDIOSEGMENT_H_
|
|
|
|
#define MOZILLA_AUDIOSEGMENT_H_
|
|
|
|
|
|
|
|
#include "MediaSegment.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2012-10-25 10:09:40 +00:00
|
|
|
#include "AudioSampleFormat.h"
|
2012-04-30 03:11:19 +00:00
|
|
|
#include "SharedBuffer.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
class AudioStream;
|
2012-11-14 19:45:33 +00:00
|
|
|
|
2012-04-30 03:11:19 +00:00
|
|
|
struct AudioChunk {
|
2012-10-25 10:09:40 +00:00
|
|
|
typedef mozilla::AudioSampleFormat SampleFormat;
|
2012-04-30 03:11:19 +00:00
|
|
|
|
|
|
|
// Generic methods
|
|
|
|
void SliceTo(TrackTicks aStart, TrackTicks aEnd)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
|
|
|
|
"Slice out of bounds");
|
|
|
|
if (mBuffer) {
|
2012-08-22 15:56:38 +00:00
|
|
|
mOffset += int32_t(aStart);
|
2012-04-30 03:11:19 +00:00
|
|
|
}
|
|
|
|
mDuration = aEnd - aStart;
|
|
|
|
}
|
|
|
|
TrackTicks GetDuration() const { return mDuration; }
|
|
|
|
bool CanCombineWithFollowing(const AudioChunk& aOther) const
|
|
|
|
{
|
|
|
|
if (aOther.mBuffer != mBuffer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mBuffer) {
|
|
|
|
NS_ASSERTION(aOther.mBufferFormat == mBufferFormat && aOther.mBufferLength == mBufferLength,
|
|
|
|
"Wrong metadata about buffer");
|
|
|
|
return aOther.mOffset == mOffset + mDuration && aOther.mVolume == mVolume;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
bool IsNull() const { return mBuffer == nullptr; }
|
2012-04-30 03:11:19 +00:00
|
|
|
void SetNull(TrackTicks aDuration)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
mBuffer = nullptr;
|
2012-04-30 03:11:19 +00:00
|
|
|
mDuration = aDuration;
|
|
|
|
mOffset = 0;
|
|
|
|
mVolume = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackTicks mDuration; // in frames within the buffer
|
|
|
|
nsRefPtr<SharedBuffer> mBuffer; // null means data is all zeroes
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mBufferLength; // number of frames in mBuffer (only meaningful if mBuffer is nonnull)
|
2012-04-30 03:11:19 +00:00
|
|
|
SampleFormat mBufferFormat; // format of frames in mBuffer (only meaningful if mBuffer is nonnull)
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mOffset; // in frames within the buffer (zero if mBuffer is null)
|
2012-04-30 03:11:19 +00:00
|
|
|
float mVolume; // volume multiplier to apply (1.0f if mBuffer is nonnull)
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of audio samples consisting of a sequence of slices of SharedBuffers.
|
|
|
|
* The audio rate is determined by the track, not stored in this class.
|
|
|
|
*/
|
|
|
|
class AudioSegment : public MediaSegmentBase<AudioSegment, AudioChunk> {
|
|
|
|
public:
|
2012-10-25 10:09:40 +00:00
|
|
|
typedef mozilla::AudioSampleFormat SampleFormat;
|
2012-04-30 03:11:19 +00:00
|
|
|
|
|
|
|
AudioSegment() : MediaSegmentBase<AudioSegment, AudioChunk>(AUDIO), mChannels(0) {}
|
|
|
|
|
|
|
|
bool IsInitialized()
|
|
|
|
{
|
|
|
|
return mChannels > 0;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
void Init(int32_t aChannels)
|
2012-04-30 03:11:19 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aChannels > 0, "Bad number of channels");
|
|
|
|
NS_ASSERTION(!IsInitialized(), "Already initialized");
|
|
|
|
mChannels = aChannels;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t GetChannels()
|
2012-04-30 03:11:19 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsInitialized(), "Not initialized");
|
|
|
|
return mChannels;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
void AppendFrames(already_AddRefed<SharedBuffer> aBuffer, int32_t aBufferLength,
|
|
|
|
int32_t aStart, int32_t aEnd, SampleFormat aFormat)
|
2012-04-30 03:11:19 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannels > 0, "Not initialized");
|
|
|
|
AudioChunk* chunk = AppendChunk(aEnd - aStart);
|
|
|
|
chunk->mBuffer = aBuffer;
|
|
|
|
chunk->mBufferFormat = aFormat;
|
|
|
|
chunk->mBufferLength = aBufferLength;
|
|
|
|
chunk->mOffset = aStart;
|
|
|
|
chunk->mVolume = 1.0f;
|
|
|
|
}
|
|
|
|
void ApplyVolume(float aVolume);
|
|
|
|
/**
|
|
|
|
* aOutput must have a matching number of channels, but we will automatically
|
|
|
|
* convert sample formats.
|
|
|
|
*/
|
2012-11-14 19:46:40 +00:00
|
|
|
void WriteTo(AudioStream* aOutput);
|
2012-04-30 03:11:19 +00:00
|
|
|
|
|
|
|
// Segment-generic methods not in MediaSegmentBase
|
|
|
|
void InitFrom(const AudioSegment& aOther)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mChannels == 0, "Channels already set");
|
|
|
|
mChannels = aOther.mChannels;
|
|
|
|
}
|
2012-07-31 12:17:21 +00:00
|
|
|
void CheckCompatible(const AudioSegment& aOther) const
|
2012-04-30 03:11:19 +00:00
|
|
|
{
|
2012-07-31 12:17:21 +00:00
|
|
|
NS_ASSERTION(aOther.mChannels == mChannels, "Non-matching channels");
|
2012-04-30 03:11:19 +00:00
|
|
|
}
|
|
|
|
static Type StaticType() { return AUDIO; }
|
|
|
|
|
|
|
|
protected:
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mChannels;
|
2012-04-30 03:11:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_AUDIOSEGMENT_H_ */
|