Files
palemoon27/dom/media/platforms/apple/AppleATDecoder.cpp
T
roytam1 93f846cd1f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1275016 - Rename Endian.h to EndianUtils.h to avoid #include confusion with Android's endian.h stdlib header. r=froydnj (b54a25f572)
- add crashreporter stuff (aa7ef15337)
- Bug 1261168 - Add AlignedAutoTArray type in Web Audio; r=padenot (285d2cb88b)
- Bug 1273390. Part 1 - move some functions to private. r=jya. (07a3037e59)
- Bug 1273390. Part 2 - add assertions. r=jya. (2cae7c596a)
- Bug 1273390. Part 3 - rename some functions to be consistent with other sub-classes of MediaDataDecoder. r=jya. (c48c7060ce)
- Bug 1273390. Part 4 - remove use of FlushableTaskQueue::Flush(). r=jya. (00565a65f4)
- Bug 1273390. Part 5 - remove use of FlushableTaskQueue. r=jya. (30600b204e)
- Bug 1273774. Part 1 - remove unused members and thread assertions. r=jya (f5177ed641)
- Bug 1273774. Part 2 - do decoding jobs synchronously without dispatching. r=jya. (62d840d27c)
- Bug 1273774. Part 3 - remove members no longer used. r=jya. (e957ca512a)
- Bug 1244410: [ffmpeg] Ensure the last drained frame has the proper duration set. r=gerald (d5521bfdd4)
- Bug 1271508. Part 1 - refactor FFmpegAudioDecoder code to be similar to FFmpegVideoDecoder::Input() so it would be easier to extract common code to the parent class. r=jya. (613e6c624c)
- Bug 1271508. Part 2 - rename functions so they are the same as those of FFmpegAudioDecoder so it would be easier to extract common code to the parent class. r=jya. (cb281cba26)
- Bug 1270350 - per comment 0, use SyncRunnable to repalce the boilerplate code. r=jya. (b99460e571)
- Bug 1271508. Part 3 - extract code to the parent class and remove use of mTaskQueue from sub-classes. r=jya. (2a7ff4dd1e)
- Bug 1274216 - remove use of FlushableTaskQueue from PlatformDecoderModule. r=jya. (eb160c5fa2)
- Bug 1271517. Part 1 - remove use of FlushableTaskQueue::Flush() from FFmpegDataDecoder::Flush(). r=jya. (fdf10da4ab)
- Bug 1271517. Part 2 - remove use of FlushableTaskQueue. r=jya. (a7016d8506)
- Bug 1273397. Part 1 - rename some functions to be consistent with other MediaDataDecoder sub-classes. r=jya. (7eecb164be)
- Bug 1273397. Part 2 - constify some members. r=jya. (e4482f9a23)
- Bug 1273397. Part 3 - remove use of FlushableTaskQueue::Flush(). r=jya. (0b7ee073fe)
- Bug 1273397. Part 4 - remove use of FlushableTaskQueue. r=jya. (6a612161d5)
- Bug 1273397. Part 5 - add assertions. r=jya. (ff3a62a6fb)
- Bug 1274199 - remove use of FlushableTaskQueue. r=cpearce. (adc4c84ede)
- Bug 1273405. Part 1 - rename some functions to be consistent with other MediaDataDecoder sub-classes. r=jya. (af123d6c21)
- Bug 1273405. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. (2d144bfbcd)
- Bug 1273405. Part 3 - remove use of FlushableTaskQueue. r=jya. (1e9ea3c2c7)
- Bug 1273405. Part 4 - add assertions. r=jya. (b400647323)
- Bug 1271491: [WMF] P1. Don't use main thread only preferences methods. r=cpearce (7177454dfb)
- Bug 1262427. Don't try D3D11 harder. r=dvander (404147d6fa)
- Use gfxConfig for D3D9 preferences. (bug 1270650, r=jrmuizel) (40d89c154c)
- Bug 1271491: P2. Allow initialization of WMFPlatformDecoderModule from any threads. r=mattwoodrow (c8fe0bf009)
- Bug 1271491: P3. Remove refcounting the number of time apple's linkers are called. r=cpearce (0324ffe876)
- Bug 1271491: [ffmpeg] P4. Remove requirements to call Init on the main thread. r=cpearce (b511d7dfd5)
- Bug 1271491: [GMP] P5. Allow GMPDecoderModule::Init() to be called off the main thread. r=cpearce (2131eb0b2e)
- Bug 1266102 - Don't run VP9 benchmark on Android r=jya (57d7b573fe)
- Bug 1271491: P6. Remove the need to call PDMFactory::Init(). r=cpearce (5726cfe49c)
- Bug 1271491: P7. Remove unused members. r=alfredo (0f8a9dde73)
- Bug 1268905 - Disable D3D11 with some Toshiba DLLs - r=cpearce (b5bf77442e)
- Bug 1269204 - Disable D3D11 with idg10umd32 9.17.10.2857 - r=cpearce (7eb6a3d96b)
- Bug 1273406 - Disable D3D11 with some iSonyVideoProcessor DLLs - r=cpearce (d9b6f0cefe)
- Bug 1273406 - Ugly macros transform into beautiful constexpr goodness - r=cpearce (0671483695)
- Bug 1273691 - Implement 'media.wmf.disable-d3d11-for-dlls' pref - r=cpearce (193ae53070)
- Bug 1272225. Part 1 - add assertions to make thread constraints clear. r=jya. (83c620470e)
- Bug 1272225. Part 2 - remove use of FlushableTaskQueue::Flush(). r=jya. (9473e092d1)
- Bug 1272553. Part 1 - move code around to be able to extract common code in P2. r=jya. (d727f97ee8)
- Bug 1272553. Part 2 - extract common code to the parent class. r=jya. (2fb3cd4bd9)
- Bug 1272553. Part 3 - make mTaskQueue private. r=jya. (93fea98cb6)
- Bug 1272232. Part 1 - move code around so we can extract common code in P2. r=jya. (8cdaab9078)
- Bug 1272232. Part 2 - extract common code to the parent class. r=jya. (27156668b3)
- Bug 1272232. Part 3 - constify some members and make them private when possible. r=jya. (550b963d97)
- Bug 1272232. Part 4 - remove use of FlushableTaskQueue::Flush(). r=jya. (bdbfdeb6bc)
- Bug 1272232. Part 5 - remove use of FlushableTaskQueue. r=jya. (640f889a9d)
- Bug 1274913 - Move PDM log definition to header. r=njn (823b07f42b)
- Bug 1275538: P1. Abort early if a skip request is in progress. r=gerald,kamidphish (d67b8a2236)
- Bug 1272422 - Part 1: Expose control of suspending background video. r=cpearce (ec7193773f)
- Bug 1272422 - Part 2: Vidoe -> Video. r=cpearce (97390aee69)
- Bug 1272422 - Part 3: Don't reset audio queue. r=jya (e183db1062)
- Bug 1272964: P1. Only activate skip to next keyframe logic when next keyframe time is known. r=gerald (1be74df027)
- Bug 1272964: P2. Don't activate skip to next keyframe until we passed the internal seek target. r=gerald (c55b6ae003)
- Bug 1258922: [MSE] P1. Initialise variable. r=gerald (56a5acb345)
- Bug 1258922: [MSE] P2. Do not go over gap when attempting to find the next key frame. r=gerald (db1319f080)
- Bug 1258922: [MSE] P3. Check that the data we are attempting to skip to is buffered. r=gerald (621d71d5d6)
- Bug 1258922: [MSE] P4. Set draining flag to true when skip to next keyframe failed. r=gerald (6c75613faf)
- Bug 1272916: [MSE] P1. Don't rely only on dts gap to establish if we have a gap in our source buffer. r=gerald (8770113b83)
- Bug 1272964: [MSE] P3. Do not skip over gaps when searching for the next keyframe. r=gerald (76916c5ac6)
- Bug 1272964: P4. Only flush decoder if skip to next keyframe actually succeeds. r=cpearce (5394708eef)
- Bug 1270323: P1. Don't reset flag indicating that new data was received. r=cpearce (d32f06ef34)
- Bug 1270323: P2. Don't process new incoming data while a skip to next keyframe is pending. r=cpearce (bca7909de9)
- Bug 1270323: [ffmpeg] P3. Use the dts of the last sample input, not the dts of the last decoded sample (0d768c33ef)
- Bug 1270323: P4. Don't drain decoder if we're already waiting for new data. r=cpearce (679302cb6e)
- Bug 1270323: P5. Prevent potential null deref. r=cpearce (cc63270e06)
- Bug 1275538: P2. Drop decoded frames that we know are already too late. r=kamidphish (4e7af9398c)
- Bug 1273018: P1. Rename some members. r=gerald (3a92fbd994)
- Bug 1273018: P2. Don't reject audio waiting promise when performing a video only seek. r=gerald (34e4988db1)
- Bug 1273018: P3. Adjust range of audio assertions. r=gerald (feb2afd0ae)
- Bug 1249706 - Backout a085ea2d24bb for blowing telemetry server's mind. r=backout (d61fb51f52)
- Bug 1249706 - Fix 8fe22dd4fc8a (backout of a085ea2d24bb). r=bustage (ba65251db7)
- Bug 1272964: [MSE] P5. Default to skipping to the next keyframe if no keyframe was found past currentTime. (29086fcf56)
- Bug 1272964: P6. Exclude frames dropped due to internal seeking from calculations. r=cpearce (bf6faa7612)
- Bug 1068151 - keep decoding a corrupted video. r=jya (3b5462e5b6)
- Bug 1273947 - Update ResetDecode() to ResetDecode(TargetQueue) r=jya (6c28d46974)
- Bug 1277508: P1. Don't attempt to demux new samples while we're currently draining. r=kamidphish (64f200b921)
- Bug 1274933: Reject data promise when EOS is encountered following waiting for data. r=gerald (5bba4a7853)
- Bug 1277508: P2. Add HasPendingDrain convenience method. r=kamidphish (3d89a90a97)
2024-10-08 23:23:56 +08:00

696 lines
22 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#include "AppleUtils.h"
#include "MP4Decoder.h"
#include "mp4_demuxer/Adts.h"
#include "MediaInfo.h"
#include "AppleATDecoder.h"
#include "mozilla/Logging.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/UniquePtr.h"
#define LOG(...) MOZ_LOG(sPDMLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
#define FourCC2Str(n) ((char[5]){(char)(n >> 24), (char)(n >> 16), (char)(n >> 8), (char)(n), 0})
namespace mozilla {
AppleATDecoder::AppleATDecoder(const AudioInfo& aConfig,
FlushableTaskQueue* aAudioTaskQueue,
MediaDataDecoderCallback* aCallback)
: mConfig(aConfig)
, mFileStreamError(false)
, mTaskQueue(aAudioTaskQueue)
, mCallback(aCallback)
, mConverter(nullptr)
, mStream(nullptr)
, mIsFlushing(false)
{
MOZ_COUNT_CTOR(AppleATDecoder);
LOG("Creating Apple AudioToolbox decoder");
LOG("Audio Decoder configuration: %s %d Hz %d channels %d bits per channel",
mConfig.mMimeType.get(),
mConfig.mRate,
mConfig.mChannels,
mConfig.mBitDepth);
if (mConfig.mMimeType.EqualsLiteral("audio/mpeg")) {
mFormatID = kAudioFormatMPEGLayer3;
} else if (mConfig.mMimeType.EqualsLiteral("audio/mp4a-latm")) {
mFormatID = kAudioFormatMPEG4AAC;
} else {
mFormatID = 0;
}
}
AppleATDecoder::~AppleATDecoder()
{
MOZ_COUNT_DTOR(AppleATDecoder);
MOZ_ASSERT(!mConverter);
}
RefPtr<MediaDataDecoder::InitPromise>
AppleATDecoder::Init()
{
if (!mFormatID) {
NS_ERROR("Non recognised format");
return InitPromise::CreateAndReject(DecoderFailureReason::INIT_ERROR, __func__);
}
return InitPromise::CreateAndResolve(TrackType::kAudioTrack, __func__);
}
nsresult
AppleATDecoder::Input(MediaRawData* aSample)
{
MOZ_ASSERT(mCallback->OnReaderTaskQueue());
LOG("mp4 input sample %p %lld us %lld pts%s %llu bytes audio",
aSample,
aSample->mDuration,
aSample->mTime,
aSample->mKeyframe ? " keyframe" : "",
(unsigned long long)aSample->Size());
// Queue a task to perform the actual decoding on a separate thread.
nsCOMPtr<nsIRunnable> runnable =
NewRunnableMethod<RefPtr<MediaRawData>>(
this,
&AppleATDecoder::SubmitSample,
RefPtr<MediaRawData>(aSample));
mTaskQueue->Dispatch(runnable.forget());
return NS_OK;
}
void
AppleATDecoder::ProcessFlush()
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
mQueuedSamples.Clear();
OSStatus rv = AudioConverterReset(mConverter);
if (rv) {
LOG("Error %d resetting AudioConverter", rv);
}
}
nsresult
AppleATDecoder::Flush()
{
MOZ_ASSERT(mCallback->OnReaderTaskQueue());
LOG("Flushing AudioToolbox AAC decoder");
mIsFlushing = true;
nsCOMPtr<nsIRunnable> runnable =
NewRunnableMethod(this, &AppleATDecoder::ProcessFlush);
SyncRunnable::DispatchToThread(mTaskQueue, runnable);
mIsFlushing = false;
return NS_OK;
}
nsresult
AppleATDecoder::Drain()
{
MOZ_ASSERT(mCallback->OnReaderTaskQueue());
LOG("Draining AudioToolbox AAC decoder");
mTaskQueue->AwaitIdle();
mCallback->DrainComplete();
return Flush();
}
nsresult
AppleATDecoder::Shutdown()
{
MOZ_ASSERT(mCallback->OnReaderTaskQueue());
LOG("Shutdown: Apple AudioToolbox AAC decoder");
mQueuedSamples.Clear();
OSStatus rv = AudioConverterDispose(mConverter);
if (rv) {
LOG("error %d disposing of AudioConverter", rv);
return NS_ERROR_FAILURE;
}
mConverter = nullptr;
if (mStream) {
rv = AudioFileStreamClose(mStream);
if (rv) {
LOG("error %d disposing of AudioFileStream", rv);
return NS_ERROR_FAILURE;
}
mStream = nullptr;
}
return NS_OK;
}
struct PassthroughUserData {
UInt32 mChannels;
UInt32 mDataSize;
const void* mData;
AudioStreamPacketDescription mPacket;
};
// Error value we pass through the decoder to signal that nothing
// has gone wrong during decoding and we're done processing the packet.
const uint32_t kNoMoreDataErr = 'MOAR';
static OSStatus
_PassthroughInputDataCallback(AudioConverterRef aAudioConverter,
UInt32* aNumDataPackets /* in/out */,
AudioBufferList* aData /* in/out */,
AudioStreamPacketDescription** aPacketDesc,
void* aUserData)
{
PassthroughUserData* userData = (PassthroughUserData*)aUserData;
if (!userData->mDataSize) {
*aNumDataPackets = 0;
return kNoMoreDataErr;
}
if (aPacketDesc) {
userData->mPacket.mStartOffset = 0;
userData->mPacket.mVariableFramesInPacket = 0;
userData->mPacket.mDataByteSize = userData->mDataSize;
*aPacketDesc = &userData->mPacket;
}
aData->mBuffers[0].mNumberChannels = userData->mChannels;
aData->mBuffers[0].mDataByteSize = userData->mDataSize;
aData->mBuffers[0].mData = const_cast<void*>(userData->mData);
// No more data to provide following this run.
userData->mDataSize = 0;
return noErr;
}
void
AppleATDecoder::SubmitSample(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
if (mIsFlushing) {
return;
}
nsresult rv = NS_OK;
if (!mConverter) {
rv = SetupDecoder(aSample);
if (rv != NS_OK && rv != NS_ERROR_NOT_INITIALIZED) {
mCallback->Error(MediaDataDecoderError::FATAL_ERROR);
return;
}
}
mQueuedSamples.AppendElement(aSample);
if (rv == NS_OK) {
for (size_t i = 0; i < mQueuedSamples.Length(); i++) {
if (NS_FAILED(DecodeSample(mQueuedSamples[i]))) {
mQueuedSamples.Clear();
mCallback->Error(MediaDataDecoderError::DECODE_ERROR);
return;
}
}
mQueuedSamples.Clear();
}
if (mTaskQueue->IsEmpty()) {
mCallback->InputExhausted();
}
}
nsresult
AppleATDecoder::DecodeSample(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
// Array containing the queued decoded audio frames, about to be output.
nsTArray<AudioDataValue> outputData;
UInt32 channels = mOutputFormat.mChannelsPerFrame;
// Pick a multiple of the frame size close to a power of two
// for efficient allocation.
const uint32_t MAX_AUDIO_FRAMES = 128;
const uint32_t maxDecodedSamples = MAX_AUDIO_FRAMES * channels;
// Descriptions for _decompressed_ audio packets. ignored.
auto packets = MakeUnique<AudioStreamPacketDescription[]>(MAX_AUDIO_FRAMES);
// This API insists on having packets spoon-fed to it from a callback.
// This structure exists only to pass our state.
PassthroughUserData userData =
{ channels, (UInt32)aSample->Size(), aSample->Data() };
// Decompressed audio buffer
AlignedAudioBuffer decoded(maxDecodedSamples);
if (!decoded) {
return NS_ERROR_OUT_OF_MEMORY;
}
do {
AudioBufferList decBuffer;
decBuffer.mNumberBuffers = 1;
decBuffer.mBuffers[0].mNumberChannels = channels;
decBuffer.mBuffers[0].mDataByteSize =
maxDecodedSamples * sizeof(AudioDataValue);
decBuffer.mBuffers[0].mData = decoded.get();
// in: the max number of packets we can handle from the decoder.
// out: the number of packets the decoder is actually returning.
UInt32 numFrames = MAX_AUDIO_FRAMES;
OSStatus rv = AudioConverterFillComplexBuffer(mConverter,
_PassthroughInputDataCallback,
&userData,
&numFrames /* in/out */,
&decBuffer,
packets.get());
if (rv && rv != kNoMoreDataErr) {
LOG("Error decoding audio stream: %d\n", rv);
return NS_ERROR_FAILURE;
}
if (numFrames) {
outputData.AppendElements(decoded.get(), numFrames * channels);
}
if (rv == kNoMoreDataErr) {
break;
}
} while (true);
if (outputData.IsEmpty()) {
return NS_OK;
}
size_t numFrames = outputData.Length() / channels;
int rate = mOutputFormat.mSampleRate;
media::TimeUnit duration = FramesToTimeUnit(numFrames, rate);
if (!duration.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples");
return NS_ERROR_FAILURE;
}
#ifdef LOG_SAMPLE_DECODE
LOG("pushed audio at time %lfs; duration %lfs\n",
(double)aSample->mTime / USECS_PER_S,
duration.ToSeconds());
#endif
AudioSampleBuffer data(outputData.Elements(), outputData.Length());
if (!data.Data()) {
return NS_ERROR_OUT_OF_MEMORY;
}
if (mChannelLayout && !mAudioConverter) {
AudioConfig in(*mChannelLayout.get(), rate);
AudioConfig out(channels, rate);
if (!in.IsValid() || !out.IsValid()) {
return NS_ERROR_FAILURE;
}
mAudioConverter = MakeUnique<AudioConverter>(in, out);
}
if (mAudioConverter) {
MOZ_ASSERT(mAudioConverter->CanWorkInPlace());
data = mAudioConverter->Process(Move(data));
}
RefPtr<AudioData> audio = new AudioData(aSample->mOffset,
aSample->mTime,
duration.ToMicroseconds(),
numFrames,
data.Forget(),
channels,
rate);
mCallback->Output(audio);
return NS_OK;
}
nsresult
AppleATDecoder::GetInputAudioDescription(AudioStreamBasicDescription& aDesc,
const nsTArray<uint8_t>& aExtraData)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
// Request the properties from CoreAudio using the codec magic cookie
AudioFormatInfo formatInfo;
PodZero(&formatInfo.mASBD);
formatInfo.mASBD.mFormatID = mFormatID;
if (mFormatID == kAudioFormatMPEG4AAC) {
formatInfo.mASBD.mFormatFlags = mConfig.mExtendedProfile;
}
formatInfo.mMagicCookieSize = aExtraData.Length();
formatInfo.mMagicCookie = aExtraData.Elements();
UInt32 formatListSize;
// Attempt to retrieve the default format using
// kAudioFormatProperty_FormatInfo method.
// This method only retrieves the FramesPerPacket information required
// by the decoder, which depends on the codec type and profile.
aDesc.mFormatID = mFormatID;
aDesc.mChannelsPerFrame = mConfig.mChannels;
aDesc.mSampleRate = mConfig.mRate;
UInt32 inputFormatSize = sizeof(aDesc);
OSStatus rv = AudioFormatGetProperty(kAudioFormatProperty_FormatInfo,
0,
NULL,
&inputFormatSize,
&aDesc);
if (NS_WARN_IF(rv)) {
return NS_ERROR_FAILURE;
}
// If any of the methods below fail, we will return the default format as
// created using kAudioFormatProperty_FormatInfo above.
rv = AudioFormatGetPropertyInfo(kAudioFormatProperty_FormatList,
sizeof(formatInfo),
&formatInfo,
&formatListSize);
if (rv || (formatListSize % sizeof(AudioFormatListItem))) {
return NS_OK;
}
size_t listCount = formatListSize / sizeof(AudioFormatListItem);
auto formatList = MakeUnique<AudioFormatListItem[]>(listCount);
rv = AudioFormatGetProperty(kAudioFormatProperty_FormatList,
sizeof(formatInfo),
&formatInfo,
&formatListSize,
formatList.get());
if (rv) {
return NS_OK;
}
LOG("found %u available audio stream(s)",
formatListSize / sizeof(AudioFormatListItem));
// Get the index number of the first playable format.
// This index number will be for the highest quality layer the platform
// is capable of playing.
UInt32 itemIndex;
UInt32 indexSize = sizeof(itemIndex);
rv = AudioFormatGetProperty(kAudioFormatProperty_FirstPlayableFormatFromList,
formatListSize,
formatList.get(),
&indexSize,
&itemIndex);
if (rv) {
return NS_OK;
}
aDesc = formatList[itemIndex].mASBD;
return NS_OK;
}
AudioConfig::Channel
ConvertChannelLabel(AudioChannelLabel id)
{
switch (id) {
case kAudioChannelLabel_Mono:
return AudioConfig::CHANNEL_MONO;
case kAudioChannelLabel_Left:
return AudioConfig::CHANNEL_LEFT;
case kAudioChannelLabel_Right:
return AudioConfig::CHANNEL_RIGHT;
case kAudioChannelLabel_Center:
return AudioConfig::CHANNEL_CENTER;
case kAudioChannelLabel_LFEScreen:
return AudioConfig::CHANNEL_LFE;
case kAudioChannelLabel_LeftSurround:
return AudioConfig::CHANNEL_LS;
case kAudioChannelLabel_RightSurround:
return AudioConfig::CHANNEL_RS;
case kAudioChannelLabel_CenterSurround:
return AudioConfig::CHANNEL_RCENTER;
case kAudioChannelLabel_RearSurroundLeft:
return AudioConfig::CHANNEL_RLS;
case kAudioChannelLabel_RearSurroundRight:
return AudioConfig::CHANNEL_RRS;
default:
return AudioConfig::CHANNEL_INVALID;
}
}
// Will set mChannelLayout if a channel layout could properly be identified
// and is supported.
nsresult
AppleATDecoder::SetupChannelLayout()
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
// Determine the channel layout.
UInt32 propertySize;
UInt32 size;
OSStatus status =
AudioConverterGetPropertyInfo(mConverter,
kAudioConverterOutputChannelLayout,
&propertySize, NULL);
if (status || !propertySize) {
LOG("Couldn't get channel layout property (%s)", FourCC2Str(status));
return NS_ERROR_FAILURE;
}
auto data = MakeUnique<uint8_t[]>(propertySize);
size = propertySize;
status =
AudioConverterGetProperty(mConverter, kAudioConverterInputChannelLayout,
&size, data.get());
if (status || size != propertySize) {
LOG("Couldn't get channel layout property (%s)",
FourCC2Str(status));
return NS_ERROR_FAILURE;
}
AudioChannelLayout* layout =
reinterpret_cast<AudioChannelLayout*>(data.get());
AudioChannelLayoutTag tag = layout->mChannelLayoutTag;
// if tag is kAudioChannelLayoutTag_UseChannelDescriptions then the structure
// directly contains the the channel layout mapping.
// If tag is kAudioChannelLayoutTag_UseChannelBitmap then the layout will
// be defined via the bitmap and can be retrieved using
// kAudioFormatProperty_ChannelLayoutForBitmap property.
// Otherwise the tag itself describes the layout.
if (tag != kAudioChannelLayoutTag_UseChannelDescriptions) {
AudioFormatPropertyID property =
tag == kAudioChannelLayoutTag_UseChannelBitmap
? kAudioFormatProperty_ChannelLayoutForBitmap
: kAudioFormatProperty_ChannelLayoutForTag;
if (property == kAudioFormatProperty_ChannelLayoutForBitmap) {
status =
AudioFormatGetPropertyInfo(property,
sizeof(UInt32), &layout->mChannelBitmap,
&propertySize);
} else {
status =
AudioFormatGetPropertyInfo(property,
sizeof(AudioChannelLayoutTag), &tag,
&propertySize);
}
if (status || !propertySize) {
LOG("Couldn't get channel layout property info (%s:%s)",
FourCC2Str(property), FourCC2Str(status));
return NS_ERROR_FAILURE;
}
data = MakeUnique<uint8_t[]>(propertySize);
layout = reinterpret_cast<AudioChannelLayout*>(data.get());
size = propertySize;
if (property == kAudioFormatProperty_ChannelLayoutForBitmap) {
status = AudioFormatGetProperty(property,
sizeof(UInt32), &layout->mChannelBitmap,
&size, layout);
} else {
status = AudioFormatGetProperty(property,
sizeof(AudioChannelLayoutTag), &tag,
&size, layout);
}
if (status || size != propertySize) {
LOG("Couldn't get channel layout property (%s:%s)",
FourCC2Str(property), FourCC2Str(status));
return NS_ERROR_FAILURE;
}
// We have retrieved the channel layout from the tag or bitmap.
// We can now directly use the channel descriptions.
layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;
}
if (layout->mNumberChannelDescriptions > MAX_AUDIO_CHANNELS ||
layout->mNumberChannelDescriptions != mOutputFormat.mChannelsPerFrame) {
LOG("Nonsensical channel layout or not matching the original channel number");
return NS_ERROR_FAILURE;
}
AudioConfig::Channel channels[MAX_AUDIO_CHANNELS];
for (uint32_t i = 0; i < layout->mNumberChannelDescriptions; i++) {
AudioChannelLabel id = layout->mChannelDescriptions[i].mChannelLabel;
AudioConfig::Channel channel = ConvertChannelLabel(id);
channels[i] = channel;
}
mChannelLayout =
MakeUnique<AudioConfig::ChannelLayout>(mOutputFormat.mChannelsPerFrame,
channels);
return NS_OK;
}
nsresult
AppleATDecoder::SetupDecoder(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
if (mFormatID == kAudioFormatMPEG4AAC &&
mConfig.mExtendedProfile == 2) {
// Check for implicit SBR signalling if stream is AAC-LC
// This will provide us with an updated magic cookie for use with
// GetInputAudioDescription.
if (NS_SUCCEEDED(GetImplicitAACMagicCookie(aSample)) &&
!mMagicCookie.Length()) {
// nothing found yet, will try again later
return NS_ERROR_NOT_INITIALIZED;
}
// An error occurred, fallback to using default stream description
}
LOG("Initializing Apple AudioToolbox decoder");
AudioStreamBasicDescription inputFormat;
PodZero(&inputFormat);
nsresult rv =
GetInputAudioDescription(inputFormat,
mMagicCookie.Length() ?
mMagicCookie : *mConfig.mExtraData);
if (NS_FAILED(rv)) {
return rv;
}
// Fill in the output format manually.
PodZero(&mOutputFormat);
mOutputFormat.mFormatID = kAudioFormatLinearPCM;
mOutputFormat.mSampleRate = inputFormat.mSampleRate;
mOutputFormat.mChannelsPerFrame = inputFormat.mChannelsPerFrame;
#if defined(MOZ_SAMPLE_TYPE_FLOAT32)
mOutputFormat.mBitsPerChannel = 32;
mOutputFormat.mFormatFlags =
kLinearPCMFormatFlagIsFloat |
0;
#elif defined(MOZ_SAMPLE_TYPE_S16)
mOutputFormat.mBitsPerChannel = 16;
mOutputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | 0;
#else
# error Unknown audio sample type
#endif
// Set up the decoder so it gives us one sample per frame
mOutputFormat.mFramesPerPacket = 1;
mOutputFormat.mBytesPerPacket = mOutputFormat.mBytesPerFrame
= mOutputFormat.mChannelsPerFrame * mOutputFormat.mBitsPerChannel / 8;
OSStatus status = AudioConverterNew(&inputFormat, &mOutputFormat, &mConverter);
if (status) {
LOG("Error %d constructing AudioConverter", status);
mConverter = nullptr;
return NS_ERROR_FAILURE;
}
if (NS_FAILED(SetupChannelLayout())) {
NS_WARNING("Couldn't retrieve channel layout, will use default layout");
}
return NS_OK;
}
static void
_MetadataCallback(void* aAppleATDecoder,
AudioFileStreamID aStream,
AudioFileStreamPropertyID aProperty,
UInt32* aFlags)
{
AppleATDecoder* decoder = static_cast<AppleATDecoder*>(aAppleATDecoder);
LOG("MetadataCallback receiving: '%s'", FourCC2Str(aProperty));
if (aProperty == kAudioFileStreamProperty_MagicCookieData) {
UInt32 size;
Boolean writeable;
OSStatus rv = AudioFileStreamGetPropertyInfo(aStream,
aProperty,
&size,
&writeable);
if (rv) {
LOG("Couldn't get property info for '%s' (%s)",
FourCC2Str(aProperty), FourCC2Str(rv));
decoder->mFileStreamError = true;
return;
}
auto data = MakeUnique<uint8_t[]>(size);
rv = AudioFileStreamGetProperty(aStream, aProperty,
&size, data.get());
if (rv) {
LOG("Couldn't get property '%s' (%s)",
FourCC2Str(aProperty), FourCC2Str(rv));
decoder->mFileStreamError = true;
return;
}
decoder->mMagicCookie.AppendElements(data.get(), size);
}
}
static void
_SampleCallback(void* aSBR,
UInt32 aNumBytes,
UInt32 aNumPackets,
const void* aData,
AudioStreamPacketDescription* aPackets)
{
}
nsresult
AppleATDecoder::GetImplicitAACMagicCookie(const MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
// Prepend ADTS header to AAC audio.
RefPtr<MediaRawData> adtssample(aSample->Clone());
if (!adtssample) {
return NS_ERROR_OUT_OF_MEMORY;
}
int8_t frequency_index =
mp4_demuxer::Adts::GetFrequencyIndex(mConfig.mRate);
bool rv = mp4_demuxer::Adts::ConvertSample(mConfig.mChannels,
frequency_index,
mConfig.mProfile,
adtssample);
if (!rv) {
NS_WARNING("Failed to apply ADTS header");
return NS_ERROR_FAILURE;
}
if (!mStream) {
OSStatus rv = AudioFileStreamOpen(this,
_MetadataCallback,
_SampleCallback,
kAudioFileAAC_ADTSType,
&mStream);
if (rv) {
NS_WARNING("Couldn't open AudioFileStream");
return NS_ERROR_FAILURE;
}
}
OSStatus status = AudioFileStreamParseBytes(mStream,
adtssample->Size(),
adtssample->Data(),
0 /* discontinuity */);
if (status) {
NS_WARNING("Couldn't parse sample");
}
if (status || mFileStreamError || mMagicCookie.Length()) {
// We have decoded a magic cookie or an error occurred as such
// we won't need the stream any longer.
AudioFileStreamClose(mStream);
mStream = nullptr;
}
return (mFileStreamError || status) ? NS_ERROR_FAILURE : NS_OK;
}
} // namespace mozilla