mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
147c5fa2eb
- Bug 1150437 - Make gmp-clearkey consistently Apache2 licensed. r=edwin,jwwang,kentuckyfriedtakahe (3c954c73dd) - Bug 1223486 - Ensure WrapTexCoord returns 0.0 instead of 1.0 when given a negative whole number. r=Bas (b4af8eb524) - Bug 1160914 - Make gmp-clearkey's decoders threadsafe refcounted, to handle DecodingComplete while GMPVideoHost::CreateFrame() is waiting. r=edwin (03359c7730) - Bug 1206763 - Enable SkiaGL canvas on gonk r=mattwoodrow (1588079b23) - Decouple CairoImage from ImageContainer. (bug 1222910, r=roc) (a93a4dad82) - Decouple D3D11ShareHandleImage from ImageContainer. (bug 1222910, r=mattwoodrow) (9642ef1c53) - Bug 1194753 - Wait longer when decoding the first frame of D3D9 DXVA video before deciding it is invalid. r=cpearce (865fe1d2f3) - Decouple D3D9SurfaceImage from ImageContainer. (bug 1222910, r=mattwoodrow) (55bc5abffc) - Bug 1191534 - Implement BlitImageToFramebuffer for IOSurface. r=jgilbert (0da6aff77f) - Decouple EGLImageImage from ImageContainer. (bug 1222910, r=snorp) (cffb7aadcb) - Decouple SurfaceTextureImage from ImageContainer. (bug 1222910, r=snorp) (5b920014d0) - Decouple MacIOSurfaceImage from ImageContainer. (bug 1222910, r=mstange) (ffab69155c) - Decouple SharedRGBImage and PlanarYCbCrImage from ImageContainer. (bug 1222910, r=mattwoodrow) (a9f6e23788) - Remove Gonk usage of ImageContainer::CreateImage. (bug 1222910, r=sotaro) (3f460b901d) - Remove CreateImage() from ImageContainer, ImageFactory, and ImageClient. (bug 1222910, r=roc) (873d5d5e47) - Bug 1212795: P1. Ensure the check if HW acceleration is allowed is performed on the main thread. r=jwwang (7cac257e5a) - Bug 1137529: Prefer Apple's VDA hardware acceleration for Hi-Def videos. r=rillian (a3ffc48e5e) - Bug 1212795: P2. Ensure all frameworks required for video decoding on mac are loaded. r=jwwang (b903b94c60) - Bug 1212795: P3. Make AppleDecoderModule detects if the required modules are loaded. r=jwwang (fb132e36b0) - Bug 1221991 - [1.3] Make SupportsMimeType a const function. r=jya (2450030808) - Bug 1221991 - [2.3] Split DecoderLoop into functions, simplify decoding state and clean up style. r=snorp (7c0e665cf7) - Bug 1221991 - [3.1] Manage module state via accessor functions. r=snorp (491b00f74b) - Bug 1221991 - [4.2] Add AndroidDecoderModule logging. r=snorp (7213505016) - Bug 1153110: Don't prefer VDA for 720p and above. r=rillian (50d5fbfa7c) - remove PR_LOGGING (44f4efbaa2) - Bug 1169653 - Limit use of the speech recognition API in JS to certified apps or apps with the proper flags set. r=smaug (c93a8949b8) - Bug 1133633: Part2. Enable async decoding on mac. r=mattmoodrow (c9740168bd) - Bug 1154896: Report decoding errors back to MP4Reader. r=rillian (26291f72e1) - Bug 1154896: Part2. Ignore kVTDecodeInfo_FrameDropped flag. r=rillian (bfca4c264e) - Bug 1198094: P1. Limit rate at which InputExhausted could be called by mac decoder. r=rillian (e5819c1666) - Bug 1214678 - Allow Apple media decoders to work on iOS r=jya (c475eff0be) - Bug 1219140 - Remove AskMediaCodecAndWait() r=bwu (dbe8d364ec) - fix includes (432c3419b0) - adapted Bug 1159509 - Support audio AMR-NB for Gonk in MP4Reader. r=jya (adaddc5c21) - Bug 1154512 - Remove MediaTaskQueue::SyncDispatch() from PDM. r=cpearce (5bddd082bd) - Bug 1204622 - release codec listener at reader task queue. r=jya (72b062b5d7) - Bug 1097498 - Wait fence for the graphic buffer. r=sotaro (c72f301939) - Bug 1198664 - Refactor: move common behaviors to base class. r=bwu,jya (a754813c5b) - Bug 1198664 - Use looper to process decoder tasks. r=bwu (768315de13) - Bug 1174721 - Use AudioCompactor for GonkAudioDecoderManager. r=sotaro (768e68c946) - Bug 1199809 - Refactor: use RAII to help manage output buffer lifecycle. r=sotaro (9a6e0dceb7) - Bug 1133955 - Record the last seek time to decide the direction to seek. r=sotaro (d0bcdbe68b) - Bug 1210045 - Fix GonkVideoDecoderManager shutdown during initialization r=bwu (b379248ff1) - Bug 1214997 - Use MozPromise in MediaCodecProxy and OMXCodecProxy r=bwu (ab9a92d640) - Bug 1199809 - Make a copy of output buffer after flush(). r=sotaro (17465ecaea) - Bug 1216895 - assert that decoder methods are run on correct thread. r=jya (b8c106c2ca) - Bug 1185018 - Part 1 of 1 - Made speech recognition services language dependent and removed assumption of a single service. r=smaug (f83acf82b4) - Bug 1207416: Add Telemetry for time spent in different Load states in WebRTC r=gcp (e3959b6760) - Bug 1219480 - Replace PRLogModuleInfo with LazyLogModule in the media directory. r=rillian (a9b13ef334) - Bug 1196558: Don't assert should output task not be dispatched. r=edwin (a2569bca73) - Bug 1199193: Ensure DrainComplete() is called once all decoded frames have been output. r=rillian (760fb42753) - Bug 1079621 - Change non-fatal errors to warnings. r=rillian (20c85e7e56) - Bug 1185234 - Implement SpeechRecognition::interimResults. r=smaug (b3d635d8c0) - Bug 1171850 - Remove 'models' dir from MODELSPS_DEST as the dir 'models' now comes from MODELSPS_FILES. r=gps (5515511e3a) - Bug 1183503 - Rename model and dictionary files for recognition in preparation for localization. r=smaug (32c78610af) - Bug 1185235 - Implement SpeechRecognition::maxAlternatives. r=smaug (91eac7f30f) - Bug 1177514 - Remove final text of 'ERROR' on recognition error, should be signaled by SpeechRecognitionError. r=smaug (c8d7b198e0) - Bug 1187791 - Part 1 of 1 - SpeechRecognition::maxAlternatives can't throw; so, rm webidl throws specifier. r=smaug (e295b2bde5) - Bug 1213131: [vpx] Configure libvpx decoder to use multi-threads decoding. r=kentuckyfriedtakahe (f38ba0ef82) - Bug 1206845: Prevent overflows in MediaCache. r=roc (e8a228dcd3) - Bug 1205825 - part 1 - don't reacquire the media cache's monitor in MediaCacheStream::FlushPartialBlockInternal; r=roc (6342592be3) - Bug 1205825 - part 2 - call Get*CachedData*Internal variants from MediaCacheStream::GetCachedRanges; r=roc (f229c8f757)
543 lines
17 KiB
C++
543 lines
17 KiB
C++
/* 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 "AppleMP3Reader.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
#include "MediaDecoder.h"
|
|
|
|
// Number of bytes we will read and pass to the audio parser in each
|
|
// |DecodeAudioData| call.
|
|
#define AUDIO_READ_BYTES 4096
|
|
|
|
// Maximum number of audio frames we will accept from the audio decoder in one
|
|
// go. Carefully select this to work well with both the mp3 1152 max frames
|
|
// per block and power-of-2 allocation sizes. Since we must pre-allocate the
|
|
// buffer we cannot use AudioCompactor without paying for an additional
|
|
// allocation and copy. Therefore, choosing a value that divides exactly into
|
|
// 1152 is most memory efficient.
|
|
#define MAX_AUDIO_FRAMES 128
|
|
|
|
using namespace mozilla::media;
|
|
|
|
namespace mozilla {
|
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
|
#define LOGE(...) MOZ_LOG(gMediaDecoderLog, mozilla::LogLevel::Error, (__VA_ARGS__))
|
|
#define LOGW(...) MOZ_LOG(gMediaDecoderLog, mozilla::LogLevel::Warning, (__VA_ARGS__))
|
|
#define LOGD(...) MOZ_LOG(gMediaDecoderLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
|
|
|
#define PROPERTY_ID_FORMAT "%c%c%c%c"
|
|
#define PROPERTY_ID_PRINT(x) ((x) >> 24), \
|
|
((x) >> 16) & 0xff, \
|
|
((x) >> 8) & 0xff, \
|
|
(x) & 0xff
|
|
|
|
AppleMP3Reader::AppleMP3Reader(AbstractMediaDecoder *aDecoder)
|
|
: MediaDecoderReader(aDecoder)
|
|
, mStreamReady(false)
|
|
, mAudioFramesPerCompressedPacket(0)
|
|
, mCurrentAudioFrame(0)
|
|
, mAudioChannels(0)
|
|
, mAudioSampleRate(0)
|
|
, mAudioFileStream(nullptr)
|
|
, mAudioConverter(nullptr)
|
|
, mMP3FrameParser(mDecoder->GetResource()->GetLength())
|
|
, mResource(mDecoder->GetResource())
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread");
|
|
}
|
|
|
|
AppleMP3Reader::~AppleMP3Reader()
|
|
{
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be on main thread");
|
|
}
|
|
|
|
|
|
/*
|
|
* The Apple audio decoding APIs are very callback-happy. When the parser has
|
|
* some metadata, it will call back to here.
|
|
*/
|
|
static void _AudioMetadataCallback(void *aThis,
|
|
AudioFileStreamID aFileStream,
|
|
AudioFileStreamPropertyID aPropertyID,
|
|
UInt32 *aFlags)
|
|
{
|
|
((AppleMP3Reader*)aThis)->AudioMetadataCallback(aFileStream, aPropertyID,
|
|
aFlags);
|
|
}
|
|
|
|
/*
|
|
* Similar to above, this is called when the parser has enough data to parse
|
|
* one or more samples.
|
|
*/
|
|
static void _AudioSampleCallback(void *aThis,
|
|
UInt32 aNumBytes, UInt32 aNumPackets,
|
|
const void *aData,
|
|
AudioStreamPacketDescription *aPackets)
|
|
{
|
|
((AppleMP3Reader*)aThis)->AudioSampleCallback(aNumBytes, aNumPackets,
|
|
aData, aPackets);
|
|
}
|
|
|
|
|
|
/*
|
|
* If we're not at end of stream, read |aNumBytes| from the media resource,
|
|
* put it in |aData|, and return true.
|
|
* Otherwise, put as much data as is left into |aData|, set |aNumBytes| to the
|
|
* amount of data we have left, and return false.
|
|
*/
|
|
nsresult
|
|
AppleMP3Reader::Read(uint32_t *aNumBytes, char *aData)
|
|
{
|
|
nsresult rv = mResource.Read(aData, *aNumBytes, aNumBytes);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
*aNumBytes = 0;
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
// XXX Maybe return a better value than NS_ERROR_FAILURE?
|
|
return *aNumBytes ? NS_OK : NS_ERROR_FAILURE;
|
|
}
|
|
|
|
nsresult
|
|
AppleMP3Reader::Init()
|
|
{
|
|
AudioFileTypeID fileType = kAudioFileMP3Type;
|
|
|
|
OSStatus rv = AudioFileStreamOpen(this,
|
|
_AudioMetadataCallback,
|
|
_AudioSampleCallback,
|
|
fileType,
|
|
&mAudioFileStream);
|
|
|
|
if (rv) {
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
|
|
struct PassthroughUserData {
|
|
AppleMP3Reader *mReader;
|
|
UInt32 mNumPackets;
|
|
UInt32 mDataSize;
|
|
const void *mData;
|
|
AudioStreamPacketDescription *mPacketDesc;
|
|
bool mDone;
|
|
};
|
|
|
|
// Error value we pass through the decoder to signal that nothing has gone wrong
|
|
// during decoding, but more data is needed.
|
|
const UInt32 kNeedMoreData = 'MOAR';
|
|
|
|
/*
|
|
* This function is called from |AudioConverterFillComplexBuffer|, which is
|
|
* called from |AudioSampleCallback| below, which in turn is called by
|
|
* |AudioFileStreamParseBytes|, which is called by |DecodeAudioData|.
|
|
*
|
|
* Mercifully, this is all synchronous.
|
|
*
|
|
* This callback is run when the AudioConverter (decoder) wants more MP3 packets
|
|
* to decode.
|
|
*/
|
|
/* static */ OSStatus
|
|
AppleMP3Reader::PassthroughInputDataCallback(AudioConverterRef aAudioConverter,
|
|
UInt32 *aNumDataPackets /* in/out */,
|
|
AudioBufferList *aData /* in/out */,
|
|
AudioStreamPacketDescription **aPacketDesc,
|
|
void *aUserData)
|
|
{
|
|
if (!aPacketDesc) {
|
|
return kAudioFileStreamError_UnspecifiedError;
|
|
}
|
|
|
|
PassthroughUserData *userData = (PassthroughUserData *)aUserData;
|
|
if (userData->mDone) {
|
|
// We make sure this callback is run _once_, with all the data we received
|
|
// from |AudioFileStreamParseBytes|. When we return an error, the decoder
|
|
// simply passes the return value on to the calling method,
|
|
// |AudioSampleCallback|; and flushes all of the audio frames it had
|
|
// buffered. It does not change the decoder's state.
|
|
LOGD("requested too much data; returning\n");
|
|
*aNumDataPackets = 0;
|
|
return kNeedMoreData;
|
|
}
|
|
|
|
userData->mDone = true;
|
|
|
|
LOGD("AudioConverter wants %u packets of audio data\n", *aNumDataPackets);
|
|
|
|
*aNumDataPackets = userData->mNumPackets;
|
|
*aPacketDesc = userData->mPacketDesc;
|
|
|
|
aData->mBuffers[0].mNumberChannels = userData->mReader->mAudioChannels;
|
|
aData->mBuffers[0].mDataByteSize = userData->mDataSize;
|
|
aData->mBuffers[0].mData = const_cast<void *>(userData->mData);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* This callback is called when |AudioFileStreamParseBytes| has enough data to
|
|
* extract one or more MP3 packets.
|
|
*/
|
|
void
|
|
AppleMP3Reader::AudioSampleCallback(UInt32 aNumBytes,
|
|
UInt32 aNumPackets,
|
|
const void *aData,
|
|
AudioStreamPacketDescription *aPackets)
|
|
{
|
|
LOGD("got %u bytes, %u packets\n", aNumBytes, aNumPackets);
|
|
|
|
// 1 frame per packet * num channels * 32-bit float
|
|
uint32_t decodedSize = MAX_AUDIO_FRAMES * mAudioChannels *
|
|
sizeof(AudioDataValue);
|
|
|
|
// descriptions for _decompressed_ audio packets. ignored.
|
|
nsAutoArrayPtr<AudioStreamPacketDescription>
|
|
packets(new AudioStreamPacketDescription[MAX_AUDIO_FRAMES]);
|
|
|
|
// This API insists on having MP3 packets spoon-fed to it from a callback.
|
|
// This structure exists only to pass our state and the result of the parser
|
|
// on to the callback above.
|
|
PassthroughUserData userData = { this, aNumPackets, aNumBytes, aData, aPackets, false };
|
|
|
|
do {
|
|
// Decompressed audio buffer
|
|
auto decoded = MakeUnique<uint8_t[]>(decodedSize);
|
|
|
|
AudioBufferList decBuffer;
|
|
decBuffer.mNumberBuffers = 1;
|
|
decBuffer.mBuffers[0].mNumberChannels = mAudioChannels;
|
|
decBuffer.mBuffers[0].mDataByteSize = decodedSize;
|
|
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(mAudioConverter,
|
|
PassthroughInputDataCallback,
|
|
&userData,
|
|
&numFrames /* in/out */,
|
|
&decBuffer,
|
|
packets.get());
|
|
|
|
if (rv && rv != kNeedMoreData) {
|
|
LOGE("Error decoding audio stream: %x\n", rv);
|
|
break;
|
|
}
|
|
|
|
// If we decoded zero frames then AudiOConverterFillComplexBuffer is out
|
|
// of data to provide. We drained its internal buffer completely on the
|
|
// last pass.
|
|
if (numFrames == 0 && rv == kNeedMoreData) {
|
|
LOGD("FillComplexBuffer out of data exactly\n");
|
|
break;
|
|
}
|
|
|
|
int64_t time = FramesToUsecs(mCurrentAudioFrame, mAudioSampleRate).value();
|
|
int64_t duration = FramesToUsecs(numFrames, mAudioSampleRate).value();
|
|
|
|
LOGD("pushed audio at time %lfs; duration %lfs\n",
|
|
(double)time / USECS_PER_S, (double)duration / USECS_PER_S);
|
|
|
|
auto samples = UniquePtr<AudioDataValue[]>(reinterpret_cast<AudioDataValue*>
|
|
(decoded.release()));
|
|
AudioData *audio = new AudioData(mResource.Tell(),
|
|
time, duration, numFrames,
|
|
Move(samples),
|
|
mAudioChannels, mAudioSampleRate);
|
|
mAudioQueue.Push(audio);
|
|
|
|
mCurrentAudioFrame += numFrames;
|
|
|
|
if (rv == kNeedMoreData) {
|
|
// No error; we just need more data.
|
|
LOGD("FillComplexBuffer out of data\n");
|
|
break;
|
|
}
|
|
} while (true);
|
|
}
|
|
|
|
bool
|
|
AppleMP3Reader::DecodeAudioData()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
// Read AUDIO_READ_BYTES if we can
|
|
char bytes[AUDIO_READ_BYTES];
|
|
uint32_t numBytes = AUDIO_READ_BYTES;
|
|
|
|
nsresult readrv = Read(&numBytes, bytes);
|
|
|
|
// This function calls |AudioSampleCallback| above, synchronously, when it
|
|
// finds compressed MP3 frame.
|
|
OSStatus rv = AudioFileStreamParseBytes(mAudioFileStream,
|
|
numBytes,
|
|
bytes,
|
|
0 /* flags */);
|
|
|
|
if (NS_FAILED(readrv)) {
|
|
mAudioQueue.Finish();
|
|
return false;
|
|
}
|
|
|
|
// DataUnavailable just means there wasn't enough data to demux anything.
|
|
// We should have more to push into the demuxer next time we're called.
|
|
if (rv && rv != kAudioFileStreamError_DataUnavailable) {
|
|
LOGE("AudioFileStreamParseBytes returned unknown error %x", rv);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
AppleMP3Reader::DecodeVideoFrame(bool &aKeyframeSkip,
|
|
int64_t aTimeThreshold)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
AppleMP3Reader::IsMediaSeekable()
|
|
{
|
|
// not used
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Query the MP3 parser for a piece of metadata.
|
|
*/
|
|
static nsresult
|
|
GetProperty(AudioFileStreamID aAudioFileStream,
|
|
AudioFileStreamPropertyID aPropertyID, void *aData)
|
|
{
|
|
UInt32 size;
|
|
Boolean writeable;
|
|
OSStatus rv = AudioFileStreamGetPropertyInfo(aAudioFileStream, aPropertyID,
|
|
&size, &writeable);
|
|
|
|
if (rv) {
|
|
LOGW("Couldn't get property " PROPERTY_ID_FORMAT "\n",
|
|
PROPERTY_ID_PRINT(aPropertyID));
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
rv = AudioFileStreamGetProperty(aAudioFileStream, aPropertyID,
|
|
&size, aData);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
|
|
nsresult
|
|
AppleMP3Reader::ReadMetadata(MediaInfo* aInfo,
|
|
MetadataTags** aTags)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
*aTags = nullptr;
|
|
|
|
/*
|
|
* Feed bytes into the parser until we have all the metadata we need to
|
|
* set up the decoder. When the parser has enough data, it will
|
|
* synchronously call back to |AudioMetadataCallback| below.
|
|
*/
|
|
OSStatus rv;
|
|
nsresult readrv;
|
|
uint32_t offset = 0;
|
|
do {
|
|
char bytes[AUDIO_READ_BYTES];
|
|
uint32_t numBytes = AUDIO_READ_BYTES;
|
|
readrv = Read(&numBytes, bytes);
|
|
|
|
rv = AudioFileStreamParseBytes(mAudioFileStream,
|
|
numBytes,
|
|
bytes,
|
|
0 /* flags */);
|
|
|
|
mMP3FrameParser.Parse(reinterpret_cast<uint8_t*>(bytes), numBytes, offset);
|
|
|
|
offset += numBytes;
|
|
|
|
// We have to do our decoder setup from the callback. When it's done it will
|
|
// set mStreamReady.
|
|
} while (!mStreamReady && !rv && NS_SUCCEEDED(readrv));
|
|
|
|
if (rv) {
|
|
LOGE("Error decoding audio stream metadata\n");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
if (!mAudioConverter) {
|
|
LOGE("Failed to setup the AudioToolbox audio decoder\n");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
if (!mMP3FrameParser.IsMP3()) {
|
|
LOGE("Frame parser failed to parse MP3 stream\n");
|
|
return NS_ERROR_FAILURE;
|
|
}
|
|
|
|
if (mStreamReady) {
|
|
aInfo->mAudio.mRate = mAudioSampleRate;
|
|
aInfo->mAudio.mChannels = mAudioChannels;
|
|
}
|
|
|
|
// This reader doesn't seem to set *aInfo = mInfo like all the others. :/
|
|
mDuration = mMP3FrameParser.GetDuration();
|
|
mInfo.mMetadataDuration.emplace(TimeUnit::FromMicroseconds(mDuration));
|
|
aInfo->mMetadataDuration.emplace(TimeUnit::FromMicroseconds(mDuration));
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
|
|
void
|
|
AppleMP3Reader::AudioMetadataCallback(AudioFileStreamID aFileStream,
|
|
AudioFileStreamPropertyID aPropertyID,
|
|
UInt32 *aFlags)
|
|
{
|
|
if (aPropertyID == kAudioFileStreamProperty_ReadyToProducePackets) {
|
|
/*
|
|
* The parser is ready to send us packets of MP3 audio.
|
|
*
|
|
* We need to set the decoder up here, because if
|
|
* |AudioFileStreamParseBytes| has enough audio data, then it will call
|
|
* |AudioSampleCallback| before we get back to |ReadMetadata|.
|
|
*/
|
|
SetupDecoder();
|
|
mStreamReady = true;
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
AppleMP3Reader::SetupDecoder()
|
|
{
|
|
// Get input format description from demuxer
|
|
AudioStreamBasicDescription inputFormat, outputFormat;
|
|
GetProperty(mAudioFileStream, kAudioFileStreamProperty_DataFormat, &inputFormat);
|
|
|
|
memset(&outputFormat, 0, sizeof(outputFormat));
|
|
|
|
// Set output format
|
|
#if defined(MOZ_SAMPLE_TYPE_FLOAT32)
|
|
outputFormat.mBitsPerChannel = 32;
|
|
outputFormat.mFormatFlags = kLinearPCMFormatFlagIsFloat;
|
|
#elif defined(MOZ_SAMPLE_TYPE_S16)
|
|
outputFormat.mBitsPerChannel = 32;
|
|
outputFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
#else
|
|
#error Unknown audio sample type
|
|
#endif
|
|
|
|
mAudioSampleRate = outputFormat.mSampleRate = inputFormat.mSampleRate;
|
|
mAudioChannels
|
|
= outputFormat.mChannelsPerFrame = inputFormat.mChannelsPerFrame;
|
|
mAudioFramesPerCompressedPacket = inputFormat.mFramesPerPacket;
|
|
|
|
outputFormat.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
// Set up the decoder so it gives us one sample per frame; this way, it will
|
|
// pass us all the samples it has in one go. Also makes it much easier to
|
|
// deinterlace.
|
|
outputFormat.mFramesPerPacket = 1;
|
|
outputFormat.mBytesPerPacket = outputFormat.mBytesPerFrame
|
|
= outputFormat.mChannelsPerFrame * outputFormat.mBitsPerChannel / 8;
|
|
|
|
OSStatus rv = AudioConverterNew(&inputFormat,
|
|
&outputFormat,
|
|
&mAudioConverter);
|
|
|
|
if (rv) {
|
|
LOGE("Error constructing audio format converter: %x\n", rv);
|
|
mAudioConverter = nullptr;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
RefPtr<MediaDecoderReader::SeekPromise>
|
|
AppleMP3Reader::Seek(int64_t aTime, int64_t aEndTime)
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
// Find the exact frame/packet that contains |aTime|.
|
|
mCurrentAudioFrame = aTime * mAudioSampleRate / USECS_PER_S;
|
|
SInt64 packet = mCurrentAudioFrame / mAudioFramesPerCompressedPacket;
|
|
|
|
// |AudioFileStreamSeek| will pass back through |byteOffset| the byte offset
|
|
// into the stream it expects next time it reads.
|
|
SInt64 byteOffset;
|
|
UInt32 flags = 0;
|
|
|
|
OSStatus rv = AudioFileStreamSeek(mAudioFileStream,
|
|
packet,
|
|
&byteOffset,
|
|
&flags);
|
|
|
|
if (rv) {
|
|
LOGE("Couldn't seek demuxer. Error code %x\n", rv);
|
|
return SeekPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
|
|
}
|
|
|
|
LOGD("computed byte offset = %lld; estimated = %s\n",
|
|
byteOffset,
|
|
(flags & kAudioFileStreamSeekFlag_OffsetIsEstimated) ? "YES" : "NO");
|
|
|
|
mResource.Seek(nsISeekableStream::NS_SEEK_SET, byteOffset);
|
|
|
|
ResetDecode();
|
|
|
|
return SeekPromise::CreateAndResolve(aTime, __func__);
|
|
}
|
|
|
|
void
|
|
AppleMP3Reader::NotifyDataArrivedInternal()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
if (!mMP3FrameParser.NeedsData()) {
|
|
return;
|
|
}
|
|
|
|
AutoPinned<MediaResource> resource(mResource.GetResource());
|
|
nsTArray<MediaByteRange> byteRanges;
|
|
nsresult rv = resource->GetCachedRanges(byteRanges);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
return;
|
|
}
|
|
|
|
IntervalSet<int64_t> intervals;
|
|
for (auto& range : byteRanges) {
|
|
intervals += mFilter.NotifyDataArrived(range.Length(), range.mStart);
|
|
}
|
|
for (const auto& interval : intervals) {
|
|
RefPtr<MediaByteBuffer> bytes =
|
|
resource->MediaReadAt(interval.mStart, interval.Length());
|
|
NS_ENSURE_TRUE_VOID(bytes);
|
|
mMP3FrameParser.Parse(bytes->Elements(), interval.Length(), interval.mStart);
|
|
if (!mMP3FrameParser.IsMP3()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
uint64_t duration = mMP3FrameParser.GetDuration();
|
|
if (duration != mDuration) {
|
|
LOGD("Updating media duration to %lluus\n", duration);
|
|
MOZ_ASSERT(mDecoder);
|
|
mDuration = duration;
|
|
mDecoder->DispatchUpdateEstimatedMediaDuration(duration);
|
|
}
|
|
}
|
|
|
|
} // namespace mozilla
|